Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression which allow only characters or space

I need help with regular expression.I need a expression in jquery which allows only character or space between two words. no double space allowed

I am using this

/^[a-zA-Z]+(-_ [a-zA-Z]+)*/

but it's not working.

Example space hello - not allowed

hello space - not allowed

hello space space hello - not allowed

space hello space - not allowed

hello1234 - not allowed

hello space 1234 - not allowed


hello world-allowed

hello-allowed

hello how are you-allowed

like image 367
Ishan Jain Avatar asked Nov 15 '12 07:11

Ishan Jain


People also ask

Can regular expressions have spaces?

The most common forms of whitespace you will use with regular expressions are the space (␣), the tab (\t), the new line (\n) and the carriage return (\r) (useful in Windows environments), and these special characters match each of their respective whitespaces.

What is the regular expression for characters?

A regular expression (shortened as regex or regexp; sometimes referred to as rational expression) is a sequence of characters that specifies a search pattern in text. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation.

How do you put a space in a regular expression?

\s stands for “whitespace character”. Again, which characters this actually includes, depends on the regex flavor. In all flavors discussed in this tutorial, it includes [ \t\r\n\f]. That is: \s matches a space, a tab, a carriage return, a line feed, or a form feed.

What do you use in a regular expression to match any 1 character or space?

Use square brackets [] to match any characters in a set. Use \w to match any single alphanumeric character: 0-9 , a-z , A-Z , and _ (underscore). Use \d to match any single digit. Use \s to match any single whitespace character.


1 Answers

you can use this

/^([a-zA-Z]+\s)*[a-zA-Z]+$/
like image 56
rahul Avatar answered Sep 20 '22 23:09

rahul