bwrrp / xspattern.js

XML Schema pattern (regular expression) engine
MIT License
10 stars 2 forks source link

crash when escaping space character #20

Open DetachHead opened 4 months ago

DetachHead commented 4 months ago
require('xspattern').compile("a\\ b")
Uncaught:
Error: Error parsing pattern "a\ b" at offset 1: expected "end of input" but found "\"

escaping a space is not necessary in javascript, but perfectly valid:

new RegExp("a\\ b") // no error

escaping spaces is required in python when using the re.VERBOSE flag, which strips all un-escaped whitespace:

re.compile('a b', re.VERBOSE).match('ab') # matches
re.compile('a\\ b', re.VERBOSE).match('ab') # doesn't match

my use case is that i'm constructing a regex in python using re.escape which is then passed to xspattern.compile, which is causing this crash.

bwrrp commented 4 months ago

Hi,

While escaping spaces is allowed (and apparently sometimes required) in Python's regex syntax, it does not seem to be allowed in the the XML Schema regex syntax implemented by this library. The list of valid escapes is given here for single-character escapes and here for multi-char escapes.

As a\ b is not a valid regex, the error is expected. A version of an re.escape-like function geared toward the XML Schema regex language could definitely be a good addition to this library, so I'll leave this open as a feature request.

DetachHead commented 3 months ago

looks like the & character has the same issue