regexrepeatsubsequencenegate

RegEx to not to contain "--"


I want to validate a string with alpha numeric values, but if the string contains -- (double dash) anywhere in the string, it should be invalid.

valid:

invalid:


https://stackoverflow.com/a/1240365/1920590

The above old post have the answer ^(?!.*bar).*$ which does the negation, but it does not work for same character repetition like --.

Can anyone help me to figure out to modify the ^(?!.*bar).*$ to identify -- as a string.


Solution

  • You may use a negative lookahead:

    ^(?!.*--)[\w-]+$
    

    RegEx Demo