[英]Javascript Regex - Get first word after substring
I have a URL like this: 我有这样的网址:
"/parameterOne/parameterTwo/parameterThree/parameterFour/ParameterFive" “/ 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'. 我想首先找到子字符串“ paramaterOne”,然后在斜杠之间的这个子字符串“ paramterTwo”之后立即找到单词。
This is the RegEx I have written so far: 这是我到目前为止编写的RegEx:
\parameterOne\b\/[^/]*[^/]*\/
It returns both 'parameterOne' and 'parameterTwo'. 它同时返回“ parameterOne”和“ parameterTwo”。 How can I modify this to only return 'parameterTwo' or is there a JavaScript solution that is better? 我该如何修改它以仅返回'parameterTwo'或有更好的JavaScript解决方案?
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. 我不想按索引查找此参数,重要的是必须先找到parameterOne,然后立即找到parameterTwo。
\/parameterOne\b\/([^/]*[^/]*)\/
You can try this and grab the value of group 1
or capture 1.See demo. 您可以尝试获取group 1
的值或捕获1。请参见演示。
https://regex101.com/r/sJ9gM7/99 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]);
You can use the javascript split
function. 您可以使用javascript split
功能。
Following code may be taken as example : 以下代码可以作为示例:
"/parameterOne/parameterTwo/parameterThree/parameterFour/ParameterFive".split("/")[2]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.