Javascript RegEx to XML Schema RegEx

这一生的挚爱 提交于 2019-12-24 00:26:23

问题


I have this Javascript pattern

^(\b[A-Z]\w*(\s|\.)*)+$

I tried using it on my XML Schema

<xs:pattern value="^(\b[A-Z][a-z]*(\s|\.)*)+$">

But when I validate I received an error saying InvalidRegex: Pattern value '^(\b[A-Z][a-z](\s|.))+$' is not a valid regular expression. The reported error was: 'This expression is not supported in the current option setting.'.

Is there a way for my Javascript pattern to work on my XML Schema pattern?


回答1:


Is not always possible to transform JS regex to XSD regex, some things like word-boundary, lookaheads and others are not supported in XSD regex, as mentioned in Michael Kay answer.

Based on your other question asking for a regex to test that all words starts with an uppercase character, you can write another regex valid for XSD, such as this one, that tests that after one (or multiple) spaces or dots the following character should not be a lowercase letter.

([^\s\.]*([\s\.]+[^a-z])?)*[\s\.]*



回答2:


Firstly, XSD regular expressions are implicitly anchored to the ends of the string, so you can (and must) omit the "^" and "$".

The more difficult problem is the \b. Outside square brackets, \b in Javascript matches a "word boundary", that is a boundary between a sequence of ASCII letters and digits, and a sequence consisting of non-(ASCII letters and digits).

Looking more carefully, I can't see what the \b actually contributes to your regex, other than ensuring there is at least one character present in the string. As far as I can see, your regex can be simplified to

[A-Za-z\s\.]+ 


来源:https://stackoverflow.com/questions/37131767/javascript-regex-to-xml-schema-regex

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!