I’m trying to make a regular expression to validate this pattern:
This is a text
[TEXT][SINGLE SPACE][TEXT][SINGLE SPACE][TEXT][SINGLE SPACE][TEXT]
A personal name
William Smith
[TEXT][SINGLE SPACE][TEXT]
Another text
[TEXT][SINGLE SPACE][TEXT][SINGLE SPACE]
The pattern will contain the next rules:
- Not start with any space
- The string can contain just a single space between words or a single space at the end of the string
I have this regular expression
/^[[A-Za-z0-9]+\s?[A-Za-z0-9]*]{0,10}$/
But I don’t know how to repeat the pattern and make it 10 of length
** Edit **
To make a more understandable, I’m working on a jQuery plugin that bind a keypress event to a input text element, and then add an expression to validate in each key pressed the text, is like a masked textbox, so to apply the rules for a mask that accept just alphanumeric characters and a space between the words I need the expression validate each key pressed like this
T
Th
This
This
This i
This is
This is
This is a
This is a
This is a t
This is a te
This is a tex
This is a text
OK, well, I think I’ve understood the requirements correctly.
First, this matches a single word followed by exactly one space:
Then we need to repeat that 10 times, right? Starting from the beginning?
But the trailing space is optional, yes?
Also, it would probably be better to use
\wrather than[a-zA-Z01-9].1 And\swould match any whitespace, which might be better than matchingonly spaces.If you meant up to 10 times, that should be
{0,9}instead of{9}.1
\wwould match underscores, as well as various international letters, which your original pattern wouldn’t. Wasn’t sure if that was intentional, so I didn’t use it, but if it wasn’t intentional then\wis a better choice.