Reputation: 3226
I need help with regular expression. I need a expression which allows only alphabets with space for ex. college name.
I am using :
var regex = /^[a-zA-Z][a-zA-Z\\s]+$/;
but it's not working.
Upvotes: 54
Views: 256354
Reputation: 1737
This works for me
function validate(text) {
let reg = /^[A-Za-z ]+$/; // valid alphabet with space
return reg.test(text);
}
console.log(validate('abcdef')); //true
console.log(validate('abcdef xyz')); //true
console.log(validate('abc def xyz')); //true
console.log(validate('abcdef123')); //false
console.log(validate('abcdef!.')); //false
console.log(validate('abcdef@12 3')); //false
Upvotes: 1
Reputation: 86
This will restrict space as first character
FilteringTextInputFormatter.allow(RegExp('^[a-zA-Z][a-zA-Z ]*')),
Upvotes: 0
Reputation: 51
This one "^[a-zA-Z ]*$" is wrong because it allows space as a first character and also allows only space as a name.
This will work perfectly. It will not allow space as a first character.
pattern = "^[A-Za-z]+[A-Za-z ]*$"
Upvotes: 4
Reputation: 41
This worked for me, simply type in javascript regex validation /[A-Za-z ]/
Upvotes: 1
Reputation: 384
This will work too,
it will accept only the letters and space without any symbols and numbers.
^[a-zA-z\s]+$
^ asserts position at start of the string Match a single character present in the list below [a-zA-z\s]
- matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy) a-z matches a single character in the range between a (index 97) and z (index 122) (case sensitive) A-z matches a single character in the range between A (index 65) and z (index 122) (case sensitive) \s matches any whitespace character (equivalent to [\r\n\t\f\v ]) $ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)
Upvotes: 2
Reputation:
Regular expression starting with lower case or upper case alphabets but not with space and can have space in between the alphabets is following.
/^[a-zA-Z][a-zA-Z ]*$/
Upvotes: 2
Reputation: 157
Upvotes: 3
Reputation: 2795
This is the better solution as it forces the input to start with an alphabetic character. The accepted answer is buggy as it does not force the input to start with an alphabetic character.
[a-zA-Z][a-zA-Z ]+
Upvotes: 35
Reputation: 143
This will accept input with alphabets with spaces in between them but not only spaces. Also it works for taking single character inputs.
[a-zA-Z]+([\s][a-zA-Z]+)*
Upvotes: 5
Reputation: 121
This will allow space between the characters and not allow numbers or special characters. It will also not allow the space at the start and end.
[a-zA-Z][a-zA-Z ]+[a-zA-Z]$
Upvotes: 12
Reputation: 1
This will work for not allowing spaces at beginning and accepts characters, numbers, and special characters
/(^\w+)\s?/
Upvotes: -2
Reputation: 93030
Just add the space to the [ ] :
var regex = /^[a-zA-Z ]*$/;
Upvotes: 110