Roland
Roland

Reputation: 9691

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.

Upvotes: 1

Views: 6216

Answers (2)

Fabrizio Calderan
Fabrizio Calderan

Reputation: 123367

you can use .test() method

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

this will find if some symbols different from a-z and A-Z 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/

Upvotes: 3

Dominic Green
Dominic Green

Reputation: 10260

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

Upvotes: 1

Related Questions