简体   繁体   中英

Regex Expression To Match Special Characters And Numbers

I'm looking for a to see if a string contains any special characters, numbers or anything else but letters.

For example I have a string "This is a 5 string #" . Now I would need a to see if this string contains any special characters like # or numbers like 5 .

I'm not familiar with using approaches.

you can use .test() method

if ("This is a 5 string #".test(/[^a-z]/i)) { ... }

this will find if some symbols different from az and AZ are inside the string. Note also that this regexp won't accept accented letters. in that case you will need a more refined regexp like

/[^a-zA-Z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF]/

see a unicode table to choose what symbols are acceptable in your string

http://unicode.org/charts/

The basics you want is something like /^[a-zA-Z]+$/ this will tell you if your string as any charachters of a to z upper and lowercase.

There are tons off resources online to learn more about regex, a good resource is http://www.regular-expressions.info/reference.html

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