问题
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