views:

39

answers:

1

Here is the regular expression that I am trying to modify:

The client only wants the user to be able to enter a maximum of 2 of the same consecutive characters.

^[a-zA-Z0-9-().\&\@\?\""#,\+\''\s\/]{7,}$
+2  A: 

You can use a look-ahead assertion:

/^(?:([a-zA-Z0-9-().&@?"#,+'\s\/])(?!\1\1)){7,}$/

Here the negated look-ahead assertion (?!\1\1) is tested for each character that is matched by ([a-zA-Z0-9-().&@?"#,+'\s\/]). It looks at the next two following characters and tests if they are the same as the previously matched one. If this is not the case, the negated look-ahead assertion is fulfilled.

Gumbo
This is a javascipt RegEx why is this in double quotes?
Amen
@Amen: I did not quote anything.
Gumbo
Amen
@Amen: You need to escape the `/` inside the regular expression.
Gumbo
I escaped it and I am still getting the error
Amen
Amen
Gumbo
I am getting an error because of the single and double quotes. The " before the # sign and the ' before the \
Amen
@Amen: You don’t need to quote it when you’re using the RegExp literals `/…/` as mentioned.
Gumbo
I think I am getting error because this following patter appears twice in the RegExp you sent:"#,+'
Amen
@Amen: `/`, `'` and `"` have no special meaning to a *regex*, but you have to escape whichever one you're using as a delimiter. So it's either `/\/'"/` (regex literal) or `new RegExp('/\'"')` or `new RegExp("/'\"/")` (constructor with string literal).
Alan Moore
Amen
@Gumbo: It would be neater to do the lookahead after, don't you think? `/^(?:([xyz])(?!\1\1)){7,}$/`
Alan Moore
@Alan Moore: Yes, definitely.
Gumbo