I'm trying to use JS regex to drop everything after a string in my url. For example www.myurl/one/two/three/?a=b&c=d
I want to drop everything after the string "three/". How would I write a regex to match this?
Asked
Active
Viewed 4,582 times
-4

Jeff
- 29
- 2
- 8
-
Do you _have to_ use a regex? It's a trivial job to find the position of that word in the string and chop there. – VLAZ Sep 23 '16 at 17:54
-
str.split('three/')[0]+'three/'; – suraj.tripathi Sep 23 '16 at 17:55
-
Do you want to delete everything after "three", or so you want to remove the query parameters? – Sep 23 '16 at 18:04
-
Instead of thinking about this as "drop everything AFTER a string", think of it as "drop everything STARTING with a string". – Sep 23 '16 at 18:13
-
Not sure why this was downvoted or marked as 'duplicate', as removing everything after an occurrence of a string is NOT the same as removing a querystring from a URL. – Greg Quinn Aug 28 '20 at 03:30
4 Answers
2
Try this one:
function getPathFromUrl(url) {
return url.split("?")[0];
}
var url = 'www.myurl/one/two/three/?a=b&c=d';
var result = getPathFromUrl(url);
alert(result);

Pradeep Jha
- 94
- 5
-
He told he wanted to drop everything after three. So this way also he can get result. – Pradeep Jha Sep 23 '16 at 18:06
-
After three, whatever parameter we will have that we can pass to the function that parameter value will get returned. – Pradeep Jha Sep 23 '16 at 18:20
-
Thanks for making me understand. I've edited again. I thought he wanted query string after three. Again I read his question then edited answer. – Pradeep Jha Sep 23 '16 at 18:34
1
Here's one quick way.
var str = 'www.myurl/one/two/three/?a=b&c=d'
var newStr = str.replace(/(.*\/three\/).*/, '$1')
alert(newStr)

Kevin Jantzer
- 9,215
- 2
- 28
- 52
1
Use built-in ability to manipulate URLs.
var a = document.createElement('a');
a.href = "http://www.myurl/one/two/three/?a=b&c=d";
a.search = '';
console.log(a.href);
Notes:
The
search
property of thea
element refers to the portion starting with the question mark.The
http://
is required here; otherwise, the URL will be interpreted as relative to the current URL.
If you would prefer to use a regexp, then you could erase everything starting with the question mark:
"www.myurl/one/two/three/?a=b&c=d".replace(/\?.*/, '')
Or, you could match what you DO want to keep, such as everything up to the question mark, using:
"www.myurl/one/two/three/?a=b&c=d".match(/.*(?=\?)/)[0]
You need the [0]
since match
returns an array, whose first element is the entire match. The ?=
here is a look-ahead. Actually that is the same as
"www.myurl/one/two/three/?a=b&c=d".match(/[^?]+/)[0]
Or, if you want to match up to three/
specifically:
"www.myurl/one/two/three/?a=b&c=d".match(/.*three\//)[0]
0
Or basicaly with methods of String and Array :
var string = "www.myurl/one/two/three/?a=b&c=d";
var array = string.split('/');
array.pop();
var result = array.join("/");
console.log(result); //www.myurl/one/two/three

kevin ternet
- 4,514
- 2
- 19
- 27
-
1This will fail if `three` is not followed by a slash, which is a perfectly valid URL format. – Sep 23 '16 at 18:11
-
-
@torazaburo and that would fail if the URL does not have a query string _and_ a final slash. – VLAZ Sep 23 '16 at 18:42
-