开发者

Regexp plurals - how?

开发者 https://www.devze.com 2022-12-16 01:32 出处:网络
I want to detect two versions of the same thing, plural and now eg: \"piece\" and \"pieces\" should both return a mat开发者_开发百科ch.

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

0

精彩评论

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