Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for only characters a-z, A-Z

I don't know how to create a regular expression in JavaScript or jQuery.

I want to create a regular expression that will check if a 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 regular expression at JavaScript RegExp Example: Online Regular Expression Tester.

like image 512
Shantanu Gupta Avatar asked Aug 20 '10 14:08

Shantanu Gupta


People also ask

How do I allow only special characters in regex?

You can use this regex /^[ A-Za-z0-9_@./#&+-]*$/.

What is the regular expression for characters?

A regex consists of a sequence of characters, metacharacters (such as . , \d , \D , \ s, \S , \w , \W ) and operators (such as + , * , ? , | , ^ ). They are constructed by combining many smaller sub-expressions.

What does AZ do in regex?

The regular expression [A-Z][a-z]* matches any sequence of letters that starts with an uppercase letter and is followed by zero or more lowercase letters. The special character * after the closing square bracket specifies to match zero or more occurrences of the character set.

How do I allow only letters and numbers in regex?

You can use regular expressions to achieve this task. In order to verify that the string only contains letters, numbers, underscores and dashes, we can use the following regex: "^[A-Za-z0-9_-]*$".


3 Answers

/^[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.

like image 125
NullUserException Avatar answered Oct 21 '22 01:10

NullUserException


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 the MDN Docs, 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.

like image 20
Hooray Im Helping Avatar answered Oct 21 '22 03:10

Hooray Im Helping


/^[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]+$");
like image 13
Ollie Edwards Avatar answered Oct 21 '22 01:10

Ollie Edwards