Regex to find whether given text not contain only email string

57 Views Asked by At

I want to write a regex (java script) to match if the given text not contain only an email string.

for ex:

I wrote a regex using (?<!…) Negative lookbehind, to check whether given text does contain only email string or not as below. But I cant use + inside lookbehind expression and it gives A quantifier inside a lookbehind makes it non-fixed width error.

regex:

^.+(?<!([a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.com))$

explanation:

  • ([a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.com) - regex to check email address.
  • ^.+(?<!([a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.com))$ - regex to check look behind of all non empty texts (.+)

Please give me an idea to resolve this issue...

Thanks

1

There are 1 best solutions below

1
blhsing On BEST ANSWER

One approach is to enclose the pattern of an email address in a negative lookahead assertion with anchors:

^(?![a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.com$)

Demo: https://regex101.com/r/gutJB8/1

Note that your example of [email protected] in the question should not match even though you claim that it should.