1

So I have this regex intended to let pass all text but those that contain as initial chars the "34" sequence:

^(?!34)(?=([\w]+))

The regex is working fine for me in https://regex101.com/r/iN1yN3/2 , check the tests to see the intended behavior.

Any Idea why it isn't working in my form?

<form>
    <input pattern="^(?!34)(?=([\w]+))" type="text">
    
        <button type="submit">Submit!</button>
</form>

2
  • 1
    Note that your example on regex101 does not work if you change the flavour to JavaScript Commented Jun 22, 2015 at 19:24
  • @JamesThorpe It does work, check the matches section. Also all the test run. The problem is that it should match the whole string. Commented Jun 26, 2015 at 10:06

1 Answer 1

4

The pattern attribute has to match the entire string. Assertions check for a match, but do not count towards the total match length. Changing the second assertion to \w+ will make the pattern match the entire string.

You can also skip the implied ^, leaving you with just:

<input pattern="(?!34)\w+" type="text">
Sign up to request clarification or add additional context in comments.

2 Comments

Fixed and improved. This is what I was looking for.
The pattern attribute has to match the entire string. This is the golden answer. Just commenting again as I had to think on it and just realize that the answer was written as the first sentence of the response. Thanks again @mcrumley

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.