dafunk dafunk - 1 year ago 83
Javascript Question

Matching strings with underscores, lowercase ASCII letters, ASCII digits, hyphens or dots only not starting with dot and hyphen

I need regular expression which could match a string like that


I created this regular expression:


However, I want to exclude a string if it only contains numbers.

How can I fix it?

Answer Source

To avoid matching a digit-only string, add a negative lookahead:


The (?![0-9]+$) lookahead is triggered only once at the beginning of the string and will try to match one or more digits up to the end of string. If they are found, a match will be failed (no match will be returned) as the lookahead is negative.