简体   繁体   中英

Javascript regex for SQL IN clause

I am pretty new to the whole regex thing and I was trying to make regex that will validate strings which contain zero or more: percentage(%) signs comma(,) signs underscore(_) signs number(0-9) signs

with max of ten numbers in a row

I came up with this: /,|%|_|[0-9]{10}/ but this regex is true for eg 123M

Please could somebody write the regex here and explain what each part of regex means ? I need the regex for SQL IN clause

You want to delimit the regex, so that it would have to match the entire string.

123M gives you a match because 123 matches and you didn't specify that you need the whole thing to match.

For this you need 2 delimiters: ^ for the beginning of the string, and $ for the end of the string, turning your regex into this:

/^[\\%_,]*[0-9]{0,10}[\\%_,]*$/

I also told it that any number, from 0 to 10, of these characters would be a match with {0,10} . I also included the string delimiters ^ and $ .

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