我想使用正则表达式匹配字符串的一部分,然后访问带括号的子字符串:

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”)有一个特殊值,所以它试图查找下一个参数的值。


当前回答

即使我同意PhiLo的正则表达式应该是:

/\bformat_(.*?)\b/

(当然,我不确定,因为我不知道正则表达式的上下文。)

其他回答

函数getMatches(字符串、正则表达式、索引){索引||(索引=1);//默认为第一个捕获组var matches=[];var匹配;while(match=regex.exec(字符串)){matches.push(match[index]);}返回匹配;}//示例:var myString='200卢比记入账户。。。2031年2月12日20:05:49(结算余额66248.77卢比)ATM。免费电话180022334418001024455(上午6点至晚上10点);var myRegEx=/清除bal.+?(\d+\.?\d{2})/gi;//获取包含每个匹配的第一个捕获组的数组var matches=getMatches(myString,myRegEx,1);//日志结果document.write(matches.length+'找到匹配项:'+JSON.stringify(匹配项))console.log(匹配项);

函数getMatches(字符串、正则表达式、索引){索引||(索引=1);//默认为第一个捕获组var matches=[];var匹配;while(match=regex.exec(字符串)){matches.push(match[index]);}返回匹配;}//示例:var myString=“something format_abc something format_def something form_ghi”;var myRegEx=/(?:^|\s)format_(.*?)(?:\s|$)/g;//获取包含每个匹配的第一个捕获组的数组var matches=getMatches(myString,myRegEx,1);//日志结果document.write(matches.length+'找到匹配项:'+JSON.stringify(匹配项))console.log(匹配项);

您的语法可能不是最好保留的。FF/Gekko将RegExp定义为Function的扩展。(FF2达到typeof(/patter/)==“函数”)

这似乎是FF特有的——IE、Opera和Chrome都有例外。

相反,使用其他人之前提到的方法:RegExp#exec或String#match。它们提供了相同的结果:

var regex = /(?:^|\s)format_(.*?)(?:\s|$)/;
var input = "something format_abc";

regex(input);        //=> [" format_abc", "abc"]
regex.exec(input);   //=> [" format_abc", "abc"]
input.match(regex);  //=> [" format_abc", "abc"]

字符串#matchAll(参见阶段3草案/2018年12月7日提案),简化了对匹配对象中所有组的访问(请记住,组0是整个匹配,而其他组对应于模式中的捕获组):

如果matchAll可用,则可以避免while循环并使用/g执行。。。相反,通过使用matchAll,您可以返回一个迭代器,您可以更方便地使用它。。。of、array spread或array.from()构造

此方法产生的输出类似于C#中的Regex.Matches,Python中的re.finditer,PHP中的preg_match_all。

查看JS演示(在Google Chrome 73.0.3683.67(官方版本),测试版(64位)中测试):

var myString=“key1:value1,key2-value2!!@key3=value3”;var matches=myString.matchAll(/(\w+)[:=-](\w*)/g);console.log([…matches]);//所有匹配捕获组值

console.log([…matches])显示

您还可以使用

let matchData=“key1:value1,key2-value2!!@key3=value3”.matchAll(/(\w+)[:=-](\w+)/g)var matches=[…matchData];//注意matchAll结果不可重复console.log(Array.from(matches,m=>m[0]));//所有匹配(组0)值//=>[“key1:value1”,“key2-value2”,“key 3=value3”]console.log(Array.from(matches,m=>m[1]));//所有匹配(组1)值//=>[“key1”,“key2”,“key 3”]

注意:请参阅浏览器兼容性详细信息。

使用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”}

只需使用RegExp$第1…$n组如:

1.匹配第一组RegExp$1.

匹配第二组RegExp$2.

如果在正则表达式中使用3组(请注意在string.match(正则表达式)之后使用)

注册Exp$1 RegExp$2 RegExp$3.

var str=“${印度}的雨很安全”;var res=str.match(/\${(.*?)\}/ig);//我在上面的例子中只使用了一个组,所以RegExp$1.console.log(RegExp.$1)

//最简单的方法是使用RegExp$1正则表达式中的第一组和第二组//注册Exp$如果存在,则在匹配后使用2var正则表达式=/\${(.*?)\}/ig;var str=“${SPAIN}的雨水主要停留在平原”;var res=str.match(正则表达式);for(res的常量匹配){var res=match.match(正则表达式);console.log(匹配);console.log(RegExp.$1)}