Extract Part Of A Url
I need to extract the hello/world part of this URL: http://example.com/#tags/hello/world I'm totally confussed with split, replace and concat. What's the best way to do it?
Solution 1:
var result = "http://example.com/#tags/hello/world".replace("http://example.com/#tags/","");
For the fastest way use this
var result = "http://example.com/#tags/hello/world".slice(25);
Solution 2:
try
functionremovePrefix(prefix,s) {
return s.substr(prefix.length);
}
removePrefix('http://example.com/#tags/','http://example.com/#tags/hello/world')
Solution 3:
Solution 4:
yourString = yourString.replace(/http:\/\/example\.com\/#tags\//, '');
Solution 5:
Split creates an array out of a string using a character as a delimiter. so that is not really what you want, since you're trying to keep the end of the string, including what would be the natural delimiter.
substr removes the first n characters of a string. so you could do
var remove_string = 'http://example.com/#tags/'
path.substr(remove_string.length);
and it would work just fine.
Replace finds a given regular expression and replaces it with the second argument. so you could also do (note that the regular expression is contained within / /)
path = path.replace(/^http:\/\/example.com\/#tags\//,"")
A little uglier because of the escaping of characters but a bit more succinct.
Also as a previous poster did you could just pass the string to be matched to replace instead of the regex.
Post a Comment for "Extract Part Of A Url"