开发者

Modify an existing regex to match '.' aswell

I'm currently using this regex ^[A-Z0-9 _]*$ to accept letters, numbers, spaces and underscores. I need to modify it t开发者_如何学Co allow .


Assuming you want to allow period (.), just add it to the character class.

^[A-Z0-9 _.]*$

A period inside a character class is treated literally which means there is not need to escape it. But escaping it as:

^[A-Z0-9 _\.]*$

is also correct and many use it. Check it out.

The following regex matches \ and . aswell.

^[A-Z0-9 _\\.]*$


To allow period? add \. inside the []

Edit: TIL inside square brackets periods don't need to be escaped in regex.

So just a ., no \

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜