问题描述:

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

I need a regular expression 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'));

相关阅读:
Top