Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IsBasicLatin and IsLatin-1Supplement as JavaScript regular expression

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?

like image 822
ToX 82 Avatar asked May 29 '15 08:05

ToX 82


People also ask

What does * do in RegEx?

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.


1 Answers

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.

like image 157
Wiktor Stribiżew Avatar answered Sep 19 '22 17:09

Wiktor Stribiżew