开发者

simple regex match

What would 开发者_运维技巧be a regex that matches "nl" OR "fr" (without the quotes) but nothing else, case insensitive?


Alternatives in regular expressions are expressed as expressions separated by the | character.

nl|fr

Case-insensitivity is specified in different ways, in different languages. One way that will work everywhere is to be explicit.

[nN][lL]|[fF][rR]

If you want "the whole string" to be one of those two phrases, then you must anchor it.

^([nN][lL]|[fF][rR])$


Give this a try:

^(nl|fr)$

And use the case insensitive flag.

And I assume you meant nl or fr, and not nlfr.


with regex, (i for case-insensitive)

/^(nl|fr)$/i 

without regex, in your favourite language, just use the equality operator

mystring == "nl" or mystring == "fr"


That's:

(nl|fr)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜