简体   繁体   中英

Match a number after a dash using regex in javascript

Hello I am trying to match a number following a dash using regex in javascript.

The sentence is as follows:

on agreeing to the resolution Agreed to by recorded vote: 238 - 182 (Roll no. 164).

I currently can select the number 238 using:

(?:\s-\s)[0-9]+

But I have not been able to find a solution for selecting 182.

Thank you for your help in advance!

You can capture each into its own group and then do something with them like this

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)."

You can also remove them, add to them, etc...

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)."

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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