Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex with space and letters only?

I made a regular expression that only accepts letters. I'm not really good in regex, thats why I don't know how to include spaces in my regex.

My HTML:

<input id="input" /> 

My js / jQuery code:

$('#input').on('keyup', function() {       var RegExpression = /^[a-zA-Z]*$/;         if (RegExpression.test($('#input').val())) {        }        else {           $('#input').val("");       } });​ 
like image 684
Aoi M. Serizawa Avatar asked Oct 08 '12 08:10

Aoi M. Serizawa


People also ask

How do you specify a space in regex?

\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 does '$' mean in regex?

Literal Characters and Sequences For instance, you might need to search for a dollar sign ("$") as part of a price list, or in a computer program as part of a variable name. Since the dollar sign is a metacharacter which means "end of line" in regex, you must escape it with a backslash to use it literally.

Is space a special character in regex?

Regex uses backslash ( \ ) for two purposes: for metacharacters such as \d (digit), \D (non-digit), \s (space), \S (non-space), \w (word), \W (non-word). to escape special regex characters, e.g., \. for . , \+ for + , \* for * , \? for ? .

Does \w include space?

A more accurate wording for \W is any Non-Alphanumeric character. \s is for Any Whitespace. Show activity on this post. \W means "non-word characters", the inverse of \w , so it will match spaces as well.


2 Answers

use this expression

var RegExpression = /^[a-zA-Z\s]*$/;   

for more refer this http://tools.netshiftmedia.com

like image 88
Pragnesh Chauhan Avatar answered Sep 29 '22 16:09

Pragnesh Chauhan


$('#input').on('keyup', function() {      var RegExpression = /^[a-zA-Z\s]*$/;        ...  }); 

\s will allow the space

like image 22
Fabrizio Calderan Avatar answered Sep 29 '22 14:09

Fabrizio Calderan