简体   繁体   中英

Regex doesn't match expected symbols

I have regex to match only digits and some specific symbols. /[()\\-\\+ \\d]/g

I try this regex with regex101.com and it match all expected symbols.

In my app i use this regex to check if user input into textbox only allowed symbols. And here is problem as only digits, space and () are allowed according to regex.

My code : http://jsfiddle.net/r6e8axsr/

You need to anchor the regex to the start and end of the string, and allow more than a single character to match. If you don't, the regex will succeed as long as there is at least one of the allowed characters in your string.

if (/^[()+ \d-]*$/.test(subject)) {
    // Successful match
} else {
    // Match attempt failed
}

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