I need help with regular expression.I need a expression in JavaScript which allows only character or space or one dot between two words, no double space allowed.
I am using this
var regexp = /^([a-zA-Z]+\s)*[a-zA-Z]+$/;
but it's not working.
Example
1. hello space .hello - not allowed
2. space hello space - not allowed
try this:
^(\s?\.?[a-zA-Z]+)+$
EDIT1
/^(\s{0,1}\.{0,1}[a-zA-Z]+)+$/.test('space ..hello space')
false
/^(\s{0,1}\.{0,1}[a-zA-Z]+)+$/.test('space .hello space')
true
v2:
/^(\s?\.?[a-zA-Z]+)+$/.test('space .hello space')
true
/^(\s?\.?[a-zA-Z]+)+$/.test('space ..hello space')
false
v3: if you need some thisn like one space or dot between
/^([\s\.]?[a-zA-Z]+)+$/.test('space hello space')
true
/^([\s\.]?[a-zA-Z]+)+$/.test('space.hello space')
true
/^([\s\.]?[a-zA-Z]+)+$/.test('space .hello space')
false
v4:
/^([ \.]?[a-zA-Z]+)+$/.test('space hello space')
true
/^([ \.]?[a-zA-Z]+)+$/.test('space.hello space')
true
/^([ \.]?[a-zA-Z]+)+$/.test('space .hello space')
false
/^([ ]?\.?[a-zA-Z]+)+$/.test('space .hello space')
true
EDIT2 Explanation:
may be problem in \s = [\r\n\t\f ]
so if only space allowed - \s?
can be replaced with [ ]?
http://regex101.com/r/wV4yY5
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