在javascript中使用正则表达式匹配破折号后的数字
您好,我正在尝试使用javascript中的正则表达式匹配破折号后面的数字 判决如下:在javascript中使用正则表达式匹配破折号后的数字,javascript,regex,Javascript,Regex,您好,我正在尝试使用javascript中的正则表达式匹配破折号后面的数字 判决如下: on agreeing to the resolution Agreed to by recorded vote: 238 - 182 (Roll no. 164). 我目前可以使用以下方法选择号码238: (?:\s-\s)[0-9]+ 但我还没有找到一个选择182的解决方案 提前感谢您的帮助 您可以将每个人捕获到自己的组中,然后像这样处理他们 string = "on agreeing to the
on agreeing to the resolution Agreed to by recorded vote: 238 - 182 (Roll no. 164).
我目前可以使用以下方法选择号码238:
(?:\s-\s)[0-9]+
但我还没有找到一个选择182的解决方案
提前感谢您的帮助 您可以将每个人捕获到自己的组中,然后像这样处理他们
string = "on agreeing to the resolution Agreed to by recorded vote: 238 - 182 (Roll no. 164)."
var regex = /(\d+)\s*\-\s*(\d+)/;
string.replace(regex, "$1 what is this doing here $2");
=> "on agreeing to the resolution Agreed to by recorded vote: 238 what is this doing here 182 (Roll no. 164)."
您还可以删除它们,添加到它们,等等
string.replace(regex, "<strong>$1</strong> - <strong>$2</strong>");
=> "on agreeing to the resolution Agreed to by recorded vote: <strong>238</strong> - <strong>182</strong> (Roll no. 164)."
string.replace(正则表达式,$1-$2);
=>“同意记录表决通过的决议:238-182(第164卷)。”
希望这能有所帮助。似乎选择了“-182”,而不是238。你想把他们两个都弄到手吗?要同时获取它们:(?:[0-9]+\s-\s)[0-9]+