Reputation: 1073
I have a URL like this:
"/parameterOne/parameterTwo/parameterThree/parameterFour/ParameterFive"
I want to firstly find the substring 'paramaterOne', then find the word immediately after this substring between the slashes which is 'paramterTwo'.
This is the RegEx I have written so far:
\parameterOne\b\/[^/]*[^/]*\/
It returns both 'parameterOne' and 'parameterTwo'. How can I modify this to only return 'parameterTwo' or is there a JavaScript solution that is better?
I do not want to find this parameter by index, it is important I have to find parameterOne first and then find parameterTwo immediately after.
Upvotes: 1
Views: 575
Reputation: 34
You can use the javascript split
function.
Following code may be taken as example :
"/parameterOne/parameterTwo/parameterThree/parameterFour/ParameterFive".split("/")[2]
Upvotes: 0
Reputation: 67968
\/parameterOne\b\/([^/]*[^/]*)\/
You can try this and grab the value of group 1
or capture 1.See demo.
https://regex101.com/r/sJ9gM7/99
var re = /\/parameterOne\b\/([^\/]*[^\/]*)\//gm;
var str = '/parameterOne/parameterTwo/parameterThree/parameterFour/ParameterFive';
var m;
if ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}
or
var myString = "/parameterOne/parameterTwo/parameterThree/parameterFour/ParameterFive";
var myRegexp = /\/parameterOne\b\/([^\/]*[^\/]*)\//gm;
var match = myRegexp.exec(myString);
alert(match[1]);
Upvotes: 2