javascriptregexxregexp

Find hash ("#") in XRegExp with comments


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.


Solution

  • 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