JavaScript is useful for a lot more than opening pop-ups. If you use HTML forms on your website, and want to make sure that your visitors submit valid data on those forms, you might want to consider using some regular expressions in JavaScript. Alejandro Gervasio explains how, with many excellent examples.
Regular expressions in JavaScript - Repetition (Page 4 of 9 )
Often, it’s useful to specify that there might be multiple occurrences of a particular string. We can represent this using the following special characters: “?”, “+” and “*”. Specifically, “?” means that the preceding character is optional, “+” means one or more of the previous character, while “*” means zero or more of the previous character.
Let’s see some examples to clarify this concept:
comput?er // Matches “computer” and “compuer”, but not “computter”.
comput+er // Matches “computer” and “computter”, but not “compuer”.
comput*er // Matches “compuer” and “computter” but not “coputer”.
^[a-zA-Z]+$ // Matches any string of one or more letters and nothing else.
Subexpressions
Sometimes, it’s good to be able to split an expression into subexpressions, so, it’s possible, for example, to represent “at least one of these strings followed by exactly one of those.” We can achieve this using parentheses and combinations of the special characters ?,+ and *, exactly as we would do in an arithmetic expression.
For example,
(good)?computer // Matches “good computer” and “computer”, but not “good good computer”.
(good)+computer // Matches “good computer” and “good good computer” but not “computer”.
(good)*computer // Matches “computer” and “good good computer” but not “good computers”.