I have a two-step validity checks, the first one is in pure JavaScript and the second one, which is not maintained by me (I don't even know what language could be), is:
<xs:pattern value="[\p{IsBasicLatin}\p{IsLatin-1Supplement}]{1,1000}" />
For a better user experience, I need to be absolutely sure that the first step validates correctly so that the second one will never throw any other error.
I did a "standard" regular expression checking for letters, numbers and so on, but still there are some differences and the second one still finds someting invalid, sometimes.
This is my actual code:
var re = /^[\wáéíóäëiöúàèììù.,;:<>_ °!?#^$€£%\(\)\[\]\=\"\'|\\\/\-\+\*\&@]+$/gm;
return re.test(value);
Is there a way to use IsBasicLatin
(and its supplement) in a JavaScript regular expression? Or how can I write a regex test that is exactly like those?
The Match-zero-or-more Operator ( * ) This operator repeats the smallest possible preceding regular expression as many times as necessary (including zero) to match the pattern. `*' represents this operator. For example, `o*' matches any string made up of zero or more `o' s.
Your second step is an XML Schema pattern that supports Unicode category classes. The pattern \p{IsBasicLatin}
stands for [\x00-\x7F]
and \p{IsLatin-1Supplement}
stands for [\x80-\xFF]
(see Unicode reference).
Also, XSD patterns are meant to match the entire input (they are "anchored by default"), so in JS you will have to enclose the pattern with ^
and $
.
So, you can translate the xs:pattern regex into JS as
^[\x00-\xFF]{1,1000}$
Or
^[\u0000-\u00FF]{1,1000}$
Mind that the limiting quantifier is used in the xs:pattern regex to allow 1 to 1000 characters. Your first regex allows unlimited number of occurrences with +
quantifier.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With