admin管理员组

文章数量:1287265

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?

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?

Share Improve this question edited May 29, 2015 at 21:44 Wiktor Stribiżew 627k41 gold badges498 silver badges611 bronze badges asked May 29, 2015 at 8:57 ToX 82ToX 82 1,0741 gold badge13 silver badges38 bronze badges 1
  • Saved on the edge of darkness. -1 to the dooms day – Steve Commented Dec 30, 2018 at 21:58
Add a ment  | 

1 Answer 1

Reset to default 10

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.

本文标签: regexIsBasicLatin and IsLatin1Supplement as JavaScript regular expressionStack Overflow