简体   繁体   中英

How to add !=-1 to regular expression?

I have a regular expression to check for non characters and only 2 digits in a string:

var RegularExpression = /^\d{2}$/

How do I modify it to accept "-1"?

Thank you

If you only want to accept any string consisting of two digits or the exact string "-1" , use an alternation ( | ), like this:

/^(\d{2}|-1)$/

Or if you'd like to accept any string consisting or two digits or a string consisting of a negative sign and one digit (ie any number from -9 to 99 ), use a character class ( […] ), like this:

/^[-\d]\d$/

像这样:

/^\d{2}$|^-1$/

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