1

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.

1
  • \/parameterOne\/([^\/]*)\/ Commented Apr 14, 2015 at 10:13

2 Answers 2

2
\/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]);
Sign up to request clarification or add additional context in comments.

2 Comments

This is returning parameterOne as well, it should only return parameterTwo
There’s still this \p which should be \/p.
0

You can use the javascript split function.

Following code may be taken as example :

"/parameterOne/parameterTwo/parameterThree/parameterFour/ParameterFive".split("/")[2]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.