简体   繁体   中英

javascript check if string contains any symbols

I have the following set of symbols:

var a = '|\/~^:,;?!&%$@*+';

How can I check is the following string contains any of those symbols?

var b = 'avguybdf';

Use RegEx

Check the how to use regex @ Javascript RegEx

As suggested, regular expressions will work.

b.match(/[|\\/~^:,;?!&%$@*+]/);

EDIT: I originally used the method here https://stackoverflow.com/a/6969486/2044733 to escape the string but because of the grouping, only the backslash character needs to be escaped.

The "/" at the beginning and end of the string are the delimiters for regular expressions in javascript, and "[]" are used to group the characters. In case you're wondering how this works.

Use RegEx. You can use test() or exec(). Read more here: http://www.w3schools.com/jsref/jsref_obj_regexp.asp

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