Package: js-regex
js-regexA compatibility layer to use Javascript regular expressions in Python.Did you know that regular expressions may vary between programming languages?
For example, let's consider the pattern"^abc$", which matches the string"abc".  But what about the string"abc\n"?  It's also matched in Python,
but not in Javascript!This and other slight differences can be really important for cross-language
standards likejsonschema, and that's whyjs-regexexists.How it worksimportreimportjs_regexre.compile("^abc$").search("abc\n")# matches, unlike JSjs_regex.compile("^abc$").search("abc\n")# does not matchInternally,js_regex.compile()replaces JS regex syntax which has a different
meaning in Python with whateverPythonregex syntax has the intended meaning.This only works for the.search()method- there is no equivalent to.match()or.fullmatch()for Javascript regular expressions.We also check for constructs which are valid in Python but not JS - such as
named capture groups - and raise an explicit error.  Constructs which are valid
in JS but not Python may also raise an error, because we're still using Python'sre.compile()function under the hood!The following table is adapted fromthis larger version,
ommiting other languages and any rows where JS and Python have the same behaviour.FeatureJavascriptPythonHandling\a(bell)noyesConverted to JS behaviour\ca-\czand\cA-\cZ(control characters)yesnoConverted to JS behaviour\dfor digits,\wfor word chars,\sfor whitespaceasciiunicodeConverted to JS behaviour (including\D,\W,\Sfor negated classes)$(end of line/string)at endallows trailing\nConverted to JS behaviour\A(start of string)noyesExplicit error, use^instead\Z(end of string)noyesExplicit error, use$instead(?<=text)(positive lookbehind)new in ES2018yesAllowed(?<!text)(negative lookbehind)new in ES2018yesAllowed(?(1)then|else)noyesExplicit error(?(group)then|else)noyesExplicit error(?#comment)noyesExplicit error(?P<name>regex)(Python named capture group)noyesNot detected (yet)(?P=name)(Python named backreference)noyesNot detected (yet)(?<name>regex)(JS named capture group)new in ES2018noError from Python, not translated (yet)$<name>(JS named backreference)new in ES2018noError from Python, not translated (yet)(?i)(case insensitive)/ionlyyesExplicit error, compile withflags=re.IGNORECASEinstead(?m)(^and$match at line breaks)/monlyyesExplicit error, compile withflags=re.MULTILINEinstead(?s)(dot matches newlines)noyesExplicit error, compile withflags=re.DOTALLinstead(?x)(free-spacing mode)noyesExplicit error, there is no corresponding mode in JavascriptBackreferences non-existent groups are an errornoyesFollows Python behaviourBackreferences to failed groups also failnoyesFollows Python behaviourNested references\1through\9yesnoFollows Python behaviourNote that in many cases Python-only regex features would be treated as part of
an ordinary pattern by JS regex engines.  Currently we raise an explicit error
on such inputs, but may translate them to have the JS behaviour in a future version.Changelog1.0.1 - 2019-10-17Allow use of native strings on Python 2.  This is not actually valid according
to the spec, but it's only going to be around for a few months so whatever.1.0.0 - 2019-10-04Now considered feature-complete and stable, as all constructs recommended
forjsonschemapatterns are supported and all Python-side incompatibilities
are detected.Compiled patterns are now cached on Python 3, exactly as forre.compile0.4.0 - 2019-10-03Now compatible with Python 2.7 and 3.5, untiltheir respective EOL dates.0.3.0 - 2019-09-30Fixed handling of non-trailing$, e.g. in"^abc$|^def$"both are convertedAdded explicit errors forre.LOCALEandre.VERBOSEflags, which have no JS equivalentAdded explicit checks and errors for use of Python-only regex features0.2.0 - 2019-09-28Convert JS-only syntax to Python equivalent wherever possible.0.1.0 - 2019-09-28Initial release, with project setup and a very basic implementation.