我需要捕获一个作为附加整数传递到 CSS 类的数字。我的正则表达式非常弱,我想要做的事情非常简单。我认为“负词边界”\B
是我想要的标志,但我想我错了
string = "foo bar-15";
var theInteger = string.replace('/bar\-\B', ''); // expected result = 15
最佳答案
使用概述的捕获组 here :
var str= "foo bar-15";
var regex = /bar-(\d+)/;
var theInteger = str.match(regex) ? str.match(regex)[1] : null;
然后你可以在任何需要使用它的地方执行if (theInteger)
关于javascript - 正则表达式捕获具有特定开头的整个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13041250/