javascriptjqueryregex

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.


Solution

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