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
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
Share
Improve this question
edited Jun 3, 2014 at 6:43
Deele
3,6842 gold badges35 silver badges52 bronze badges
asked Jun 3, 2014 at 5:06
user3291547user3291547
731 gold badge1 silver badge4 bronze badges
2
- $ - means end of string, also no dash in regex. what actually you need to get? – Subdigger Commented Jun 3, 2014 at 5:12
- Ending space, also no dash Example testing.t not allowed – user3291547 Commented Jun 3, 2014 at 5:14
4 Answers
Reset to default 7try 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./r/wV4yY5
This regular expression will match multiple spaces or dots between words and spaces before the first word or after the last word. This is the opposite of what you want, but you can always invert it (!foo.match(...)
):
/\b[\. ]{2,}\b|^ | $/
In regex101.: http://regex101./r/fT0pF2
And in plainer english:
\b => a word boundary
[\. ] => a dot or a space
{2,} => 2 or more of the preceding
\b => another word boundary
| => OR
^{space} => space after string start
| => OR
{space}$ => space before string end
This will match:
"this that" // <= has two spaces
"this. that" // <= has dot space
" this that" // <= has space before first word
"this that " // <= has space after last word
But it will not match:
"this.that and the other thing"
How about that?
^\s*([a-zA-Z]+\s?\.?[a-zA-Z]+)+$
This allows:
- Multiple leading spaces.
- Space as a delimiter.
- Dot in the second and any consecutive word.
Try this (only a single whitespace or a period is allowed between the words):
> /\w+[\s\.]\w+/.test('foo bar')
true
> /\w+[\s\.]\w+/.test('foo.bar')
true
> /\w+[\s\.]\w+/.test('foo..bar')
false
> /\w+[\s\.]\w+/.test('foo .bar')
false