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
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.
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.
\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.
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.
you can use this
/^([a-zA-Z]+\s)*[a-zA-Z]+$/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With