views:

115

answers:

4

I dont know how to create regular expression in javascript or Jquery

I want to create a regular expression that will check if string contains only characters between a-z and A-Z with any arrangement

EDIT

When I tried to make regex

/^[a-zA-Z\s]+$/

to accept white spaces as well. It is not working. What could be the mistake.

I am testing my regex here

+3  A: 
/^[a-zA-Z]+$/ 

Off the top of my head.

Edit:

Or if you don't like the weird looking literal syntax you can do it like this

new RegExp("^[a-zA-Z]+$");
Ollie Edwards
I missed the ^ and $, I'll edit my answer to make it correct but props to @NullUserException for getting it first
Ollie Edwards
+6  A: 
/^[a-zA-Z]*$/

Change the * to + if you don't want to allow empty matches.

References:

Character classes ([...]), Anchors (^ and $), Repetition (+, *)

The / are just delimiters, it denotes the start and the end of the regex. One use of this is now you can use modifiers on it.

NullUserException
@NullUser: why ^ and $ has been used. What is its purpose
Shantanu Gupta
@Shantanu: Those are used to match the beginning and the end of the string. Without them any string containing letters would be matched. Now only strings containing ONLY letters will be matched.
Jakob
@Shantanu: ^ is begin of string, $ is end of string. It's used here to make sure the complete string does contain characters. If you would do `/[a-zA-Z]*/` if would also match the string "1234 fo!" (at "fo").
DarkDust
+1  A: 
/[:alpha:]+/

Any alpha character A to Z or a to z.

or

/^[[:alpha:]]+$/s

to match strictly with spaces.

RobertPitt
But... shouldn't this be /[[:alpha:]]+/ ?The regular expression [a-zA-Z] will return incorrect values for some character encodings (e.g. EBCDIC, where there is a gap between i and j, and another between r and s (which includes the ~ character), and between I and J and between R and S. This one works everywhere.
James McLeod
`[[:alpha:]]` is correct, but lots of languages will just drop a warning and DWYM. or it might just be untested.
sreservoir
@James, arguably, if the question is taken literally, `[a-zA-Z]` will give correct results even for EBCDIC, and `[[:alpha:]]` will return incorrect results for EBCDIC. Because the question did not ask for only letters. It asked for a-z, A-Z. :-/
LarsH
@LarsH - that's a very (in fact annoyingly) good point, but I think I'll stick with my interpretation. Who really wants to include ~, }, and a bunch of unprintables with a-zA-Z?
James McLeod
+3  A: 

Piggybacking on what the other answers say, since you don't know how to do them at all, here's an example of how you might do it in JavaScript:

var charactersOnly = "This contains only characters";
var nonCharacters = "This has _@#*($()*@#$(*@%^_(#@!$ non-characters";

if (charactersOnly.search(/[^a-zA-Z]+/) === -1) {
  alert("Only characters");
}

if (nonCharacters.search(/[^a-zA-Z]+/)) {
  alert("There are non characters.");
}

The / starting and ending the regular expression signify that it's a regular expression. The search function takes both strings and regexes, so the / are necessary to specify a regex.

From this page, the function returns -1 if there is no match.

Also note: that this works for only a-z, A-Z. If there are spaces, it will fail.

Hooray Im Helping
+1 for giving the answer in JavaScript :)
Patrick McDonald