Regexp plurals - how?
I want to detect two versions of the same thing, plural and now
eg:
"piece" and "pieces" should both return a mat开发者_开发百科ch.
var re_1 = new RegExp("how many piece", "i");
or
var re_1 = new RegExp("how many pieces", "i");
How do I form a RegExp expression in Javascript to detect both singular and plural in the same pattern?
>>> /how many pieces?/.test('how many piece')
true
>>> /how many pieces?/.test('how many pieces')
true
>>> /how many pieces?/.test('how many piecez')
true
>>> /how many pieces?/.test('how many piec')
false
The question mark denotes that the prior character is optional. You shouldn't need to construct a new RegExp
unless you're dynamically generating it, feel free to add any additional options after the end /
( g for global, i for case insensitive ).
精彩评论