开发者

Simple JavaScript Regular Expression

I'm sure this is really simple but I haven't been able to create one that works.

I need a regular expre开发者_Python百科ssion to extract a one or two digit number 1-13 from a string such as "(11)" or "(3)"

Thanks :)


result = subject.match(/\b(?:1[0-3]|0?[1-9])\b/);

will match a two digit number between 1 and 13, wherever it may be (as long as it's not part of a longer number or within a word).

If you want to hard-code the parentheses, it's

result = subject.match(/\((?:1[0-3]|0?[1-9])\)/);

and if you want to find more than one match in a single string, use the g modifier (after the last slash).


var theNumber = parseInt(theString.replace(/\(([1-9]|1[1-3])\)/, '$1'));

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜