Find hash (“#”) in XRegExp with comments
-
24-07-2021 - |
Domanda
If you use the 'x' flag of XRegExp, it is unclear how to add the literal '#' to the search.
The documentation does not appear to yield any illumination.
For example:
> XRegExp("the # hey",'x').toString()
'/the(?:)/'
This is as expected - everything after '#' is a comment and ignored.
The apparent solution, not ignoring the #
and following contents on that line, would usually be (based on my limited experience) leading with an escape modifier, eg \\
:
> XRegExp("the \# hey",'x').toString()
'/the(?:)/'
or
> XRegExp("the \\# hey",'x').toString()
'/the(?:)\\#(?:)hey/'
Unfortunately neither works as expected, the prior for obvious reasons, and the latter yields:
> XRegExp("the \\# hey",'x').xtest("the # hey")
false
or even in the more pernicious case that we must modify input data:
> XRegExp("the \\# hey",'x').xtest("the \\# hey")
false
The alternative canonical escaping would be using a double-hash (##
), but that does not work either:
> XRegExp("the ## hey",'x').toString()
'/the(?:)/'
So the question becomes, how can one add a #
literal to an XRegExp expression that uses the 'x' flag? I'm stumped.
Soluzione
I found the answer and please note that my tests above would not work because the spaces should be replaced by \\s+
.
The answer seems to be to use [#]
e.g.
> XRegExp("the\\s+[#]\\s+hey",'x').xtest("the # hey")
true
Altri suggerimenti
The documentation for flag x
is at http://xregexp.com/flags/#extended.
One of the effects of x
is that whitespace in your regex pattern is ignored. I see you've discovered that the literal spaces in your regex can be replaced with \s
(\\s
when embedded in a string literal due to string escaping rules). But note that that's changing your pattern from matching just a space char to matching any whitespace char. Alternatively you could just escape the spaces: XRegExp('the\\ \\# \\ hey', 'x')
. Or you could use [ ]
in the same way you used [#]
. Or you could do something like XRegExp('the' + XRegExp.escape(' # ') + 'hey', 'x')
. Or just not use the x
flag if you have literal whitespace and #
chars in your pattern.