繁体   English   中英

在javascript中使用正则表达式在破折号后匹配数字

[英]Match a number after a dash using regex in javascript

您好,我正在尝试在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 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)."

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM