0

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.

4
  • 2
    without your input we will not be able to help you Commented Dec 18, 2015 at 10:02
  • the regex101 has a unit test with some possible matches. The jfiddle has *.com that is one of the matches that passes in the regex101 but not in the html. Commented Dec 18, 2015 at 10:07
  • it is passing in given fiddle Commented Dec 18, 2015 at 10:08
  • oi65.tinypic.com/2md2b01.jpg It doens't pass it for me Commented Dec 18, 2015 at 10:18

1 Answer 1

3

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)

Sign up to request clarification or add additional context in comments.

6 Comments

This seems to solve the issue. Just wondering if you could help me out by making the regex work so this works *.com but for example this doesn't *<com> or *!com! In any case i will select your answer because i now understand why it doesn't work.
@jermey: I have tested *<com> and *!com! in your fiddle and it works too. If you don't want to validate these two cases, replace .* with something more explicit.
I would like them not to work. Basically this part \*.*| should be changed so the regex says something like: The beginning of the string can be a * but the rest should still follow the current regex in my original post - \*.*| Instead of what it says now. if its starts with a * then ignore the entire regex
@jermey: I see. In this case try to change your pattern like this: (?!.{255})(?:\*|(?:\*\.)?(?:[A-Za-z_{}-][\w{}-]{0,62}\.?)+[A-Za-z{}]+) that allows a single * or all the subpattern starting with an optional *. (if you don't need the dot, replace (?:\*\.)? with \*?)
Thank you it works perfectly. This showed me how helpless i am when it comes to regex. Need to go find a tutorial and practice when i have the time. :(
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.