我想使用正则表达式匹配字符串的一部分,然后访问带括号的子字符串:
var myString=“something format_abc”;//我要“abc”var arr=/(?:^|\s)format_(.*?)(?:\s|$)/.exec(myString);控制台日志(arr);//打印:[“format_abc”,“abc”]。。到现在为止,一直都还不错。控制台日志(arr[1]);//打印:未定义(???)控制台日志(arr[0]);//打印:format_undefined(!!!)
我做错了什么?
我发现上面的正则表达式代码没有任何问题:我测试的实际字符串是这样的:
"date format_%A"
报告“%A”未定义似乎是一种非常奇怪的行为,但它与这个问题没有直接关系,所以我打开了一个新的问题,为什么匹配的子字符串在JavaScript中返回“undefined”?。
问题是console.log像printf语句一样接受其参数,因为我记录的字符串(“%a”)有一个特殊值,所以它试图查找下一个参数的值。
使用es2018,您现在可以对命名组使用String.match(),使正则表达式更明确地说明它要做什么。
const url =
'https://stackoverflow.com/questions/432493/how-do-you-access-the-matched-groups-in-a-javascript-regular-expression?some=parameter';
const regex = /(?<protocol>https?):\/\/(?<hostname>[\w-\.]*)\/(?<pathname>[\w-\./]+)\??(?<querystring>.*?)?$/;
const { groups: segments } = url.match(regex);
console.log(segments);
你会得到类似的东西
{protocol:“https”,主机名:“stackerflow.com”,路径名:“questions/432493/how-do-you-acess-the-matched-groups-in-a-javascript-regular-expression”,querystring:“some=parameter”}
关于上面的多匹配括号示例,我在没有得到我想要的答案后,在这里寻找答案:
var matches = mystring.match(/(?:neededToMatchButNotWantedInResult)(matchWanted)/igm);
在查看了上面while和.push()的稍微复杂的函数调用之后,我发现这个问题可以用mystring.replace()非常优雅地解决(替换不是重点,甚至还没有完成,第二个参数的CLEAN内置递归函数调用选项是!):
var yourstring = 'something format_abc something format_def something format_ghi';
var matches = [];
yourstring.replace(/format_([^\s]+)/igm, function(m, p1){ matches.push(p1); } );
在这之后,我想我再也不会用.match()做任何事情了。