jermey
jermey

Reputation: 179

regex works in regex tester but not in pattern

Its quite a simple but in my opinion weird problem i basically have this regex and entered a few tests and they work.

(?=^\*)|(?=^.{1,254}$)(^(?:(?!\d+\.)[a-zA-Z0-9_\-\{\}]{1,63}\.?)+(?:[a-zA-Z\{\}]{1,})$)

https://regex101.com/r/hU6tP0/2

But when i try to use it in html it fails. But if i test it in javascript it works.

http://jsfiddle.net/ek6kby2q/9/

I don't have much knowledge at all about regex so maybe anyone know whats going wrong or got any tips to make the regex better is welcome.

Upvotes: 0

Views: 479

Answers (1)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89639

As an html attribute, the pattern must match all the string from the beginning to the end, that's why (?=^\*) fails to do it, since it matches zero characters.

Use this pattern instead:

\*.*|(?!.{255})(?:[A-Za-z_{}-][\w{}-]{0,62}\.?)+[A-Za-z{}]+

(You can omit the anchors since they are implicit)

Upvotes: 3

Related Questions