admin管理员组

文章数量:1431693

I'm looking for a regexp 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 regexp to see if this string contains any special characters like # or numbers like 5.

I'm not familiar with using regexp approaches.

I'm looking for a regexp 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 regexp to see if this string contains any special characters like # or numbers like 5.

I'm not familiar with using regexp approaches.

Share Improve this question edited Nov 4, 2012 at 2:10 brettdj 55.8k16 gold badges118 silver badges178 bronze badges asked Dec 31, 2011 at 12:58 RolandRoland 9,74119 gold badges82 silver badges136 bronze badges 3
  • 2 Have you had a look at regular-expressions.info and tried to learn the basics? – Felix Kling Commented Dec 31, 2011 at 13:01
  • 1 What are your needs exactly then? Because what you just stated is pletely googleable and found in every basic regex tutorial – Esailija Commented Dec 31, 2011 at 13:03
  • I'm reading now some tutorials on regex expressions. I found something similar to what I need, in fact it is very basic, only that first I had the impression that I must look for the numbers and special characters in the string, only to find that I should only match letters from a-z A-Z ... – Roland Commented Dec 31, 2011 at 13:33
Add a ment  | 

2 Answers 2

Reset to default 3

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/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

本文标签: javascriptRegex Expression To Match Special Characters And NumbersStack Overflow