I need to double up the backslashes in a string, and have found an odd limitation to Javascript: it's not possible to enter strings without evaluating escape characters. The behaviour means I can't distinguish via input
"\!" == "!" // true
"\?" == "?" // true
"\d" == "d" // true
"\!\[\d\d\:\d\d\:\d\d.*?\]\(" == ". The string evaluation leads to them being stored identically, as for \b
and b
.
"\![d\d \bb" // == "![dd bb]" etc.
I want to create a new RegExp
, composed from strings (with doubling up of escape characters as required) and variables, to be able to process a series of files without manually changing the regular expression each time, or relying on some weaker, more generic regex.
It really looks like I'll have to do this by hand, despite the backslashes being right there to be replaced against. I'm confused by the lack of any obvious way I could do the same for a regular expression
Without the ability to enter backslashes (as far as I know) this doesn't seem possible - or rather the solution would be hacky and not programmatic.
Does anyone know of the function I'm looking for? It seems quite fundamental so I'm hoping I've missed something obvious!
Observations like the length of \!\[
returning 2 rather than 4 make me feel it might actually be some fundamental fact of the language, but then common sense tells me... this page is being processed in Javascript. Sure enough, fishing out the previous code block :
document.querySelector('... > code')[0].innerText
// "\!\["
document.querySelector('... > code')[0].innerText.length
... returns 4 rather than 2 - so there must surely be a way to enter strings like this?
Related question How do I Programmatically create a double escape?