slevithan / xregexp

Extended JavaScript regular expressions
http://xregexp.com/
MIT License
3.31k stars 278 forks source link

html input pattern #340

Closed eljeffeg closed 2 years ago

eljeffeg commented 2 years ago

I'm trying to replace an input pattern with a unicode equivalent, something like ^[0-9\\p{L} _\\-\\.]{3,16}$
<input type="text" pattern="^[a-zA-Z0-9_\-\.]{3,16}$"> The form automatically handles this when you click submit, telling the user that the pattern was not matched. Is there a way for xregexp to take over the default pattern handling here or what is the best way to do this?

slevithan commented 2 years ago

XRegExp('...pattern...').source will give you a native JS pattern that you can use elsewhere. But since the input element's pattern attribute takes a string and not JS, you'd have to set it via JS.