简体   繁体   中英

How to allow Regex expression with Spaces

var validate = /^[@#&%][a-zA-Z0-9]{4}$/;

我还需要在此正则表达式中允许使用空格。

It's exactly as easy as one might think: add a space to the regex where you want to match a space. Spaces work just like any other character in regex.

If you want to match any whitespace character (including tabs, etc.) you can use \\s to match any whitespace character.

if you want just regular space to be allowed then use a normal space in your brackets like this

/^[@#&%][a-zA-Z0-9 ]{4}$/

else if you wish to allow all white spaces such as tab or new line use \\s :

/^[@#&%][a-zA-Z0-9\s]{4}$/

You need to do this:

var validate = /^[@#&%][a-zA-Z0-9 ]{4}$/;

Note, just add a space within the brackets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM