469,277 Members | 1,959 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,277 developers. It's quick & easy.

Javascript regular expression for a subject field

JnrJnr
88
Hi people, I have this regex?
Expand|Select|Wrap|Line Numbers
  1. /^[a-zA-Z0-9]+[\s]*[a-zA-Z0-9]+$/
I want the field to start with alphanumeric and allow single white spaces (or no white spaces) zero or more times and zero or more times in the sentence and then end with alphanumeric.
For example:
1. Hello John123
2. Hello John123 (with maybe 3 spaces between hello and John 123)
3. Hello John123 how are you

Help would be appreciated!
Aug 31 '11 #1

✓ answered by Dormilich

you can use parentheses (groups) to get that repetition.
Expand|Select|Wrap|Line Numbers
  1. // simplified example
  2. /^\w(?:\s?\w)+$/

6 1298
Dormilich
8,651 Expert Mod 8TB
you can use parentheses (groups) to get that repetition.
Expand|Select|Wrap|Line Numbers
  1. // simplified example
  2. /^\w(?:\s?\w)+$/
Aug 31 '11 #2
JnrJnr
88
Thanks it works for me but could you please explain how the parentheses or groups work? Im still new to regex
Aug 31 '11 #3
Dormilich
8,651 Expert Mod 8TB
well, it’s just that, a group. nothing complicated there.
Aug 31 '11 #4
JnrJnr
88
Anything desired to know that needs understanding is complicated till it is known.
Thanks again
Aug 31 '11 #5
Dormilich
8,651 Expert Mod 8TB
some things are so simple, that it’s hard to explain in a simpler way. other than that I can only refer to RegExp tutorials like this one.
Aug 31 '11 #6
JnrJnr
88
There are many tutorials and quick starts on regular expressions but I just did not know how the parentheses or groups work. A bit better link would be this http://www.regular-expressions.info/brackets.html
tanx
Aug 31 '11 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by John . | last post: by
1 post views Thread by NvrBst | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.