-7

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.*?\]\(" == "![dd:dd:dd.*?](" // true

Note here that \d and d are strings with different meanings in a regex (digit vs. the letter 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?

Community
  • 1
  • 1
Louis Maddox
  • 5,226
  • 5
  • 36
  • 66
  • Why not RegEx literals? – thefourtheye Mar 19 '15 at 02:22
  • How about `"\\\\d"`? – elclanrs Mar 19 '15 at 02:24
  • You've missed the point of my question @elclanrs - I want to enter a string and have any escaped character become double escaped. Replace does not work as outlined above – Louis Maddox Mar 19 '15 at 02:28
  • You say you want to double up the backslashes in a string. Suppose you have a string that would be represented by the string literal `"\n"`. Do you want that to become `"\\n"`, or do you want to leave it unchanged? – user2357112 Mar 19 '15 at 02:30
  • If I understand correctly, you cannot do this programatically, and escaped character is just a character. It is part of the syntax, not the string. – elclanrs Mar 19 '15 at 02:33

3 Answers3

1

So after reading your question many times I think I get where you are going, and it definitely sounds like an XY problem. An escaped character is just a character; escaping is part of the syntax, not the string itself. If you are building regex dynamically, you will have to double escape special characters. In any case, here's a hacky way to do what you want relying on function stringification:

var regex = function(f) {
  return RegExp(f.toString().match(/return\s+?'([\s\S]+)'/).pop())
}

// instead of `RegExp('\\d\\b')`
regex(function(){return '\d\b'}) //=> /\d\b/

But again, this is not the way to go; just double-escape your characters manually.

elclanrs
  • 92,861
  • 21
  • 134
  • 171
  • The thing is, double escaping is what's hacky. The syntax vs. string difference is what I'm getting at above yes. This answer's neat and all, but doesn't give back a double escaped string suitable for the `new RegExp` constructor (i.e. components of rather than the complete regular expressions) like I was pondering in the question. I'll work with this and come up with something in the morning if noone else has solved it (entirely likely now that it's been thoroughly downvoted) – Louis Maddox Mar 19 '15 at 03:01
  • 1
    You don't have to generate the regex right away, the point of stringifying the function is the get the string double escape already, so if you just return the matched string, you can concatenate it to something else. – elclanrs Mar 19 '15 at 03:16
0

You know you can double-escape if you want to store the actual backslash, right?

"\\ha!" // Will result in '\ha'
bvaughn
  • 13,300
  • 45
  • 46
  • You've missed the point of my question. Yes I'm aware of this, the question is to do so programmatically – Louis Maddox Mar 19 '15 at 02:26
  • @Louis: Where is the string with the backslash coming from? Most ways of getting a string won't interpret escape sequences; for example, if you enter `\d` into a text field and read the contents, you'll get a string with a literal backslash and a d in it. – user2357112 Mar 19 '15 at 02:28
  • I... have no idea at all what your question is then. Maybe try to reword it? At the point at which the strings already exist... the characters are already set. – bvaughn Mar 19 '15 at 02:29
  • I'm aware that characters are set and went to lengths to point out that that's the case. I think you just rushed to answer without reading my question, in which I mentioned doubling backslashes twice (you also missed the `!` in your code comment). I don't think rewording's the issue, just a need for some lateral thinking and giving responses a little more thought. elclanrs's answer approaches what I'm getting at, using a less standard way of entering the string. – Louis Maddox Mar 19 '15 at 03:14
  • 2
    No... I read your question. It was just poorly worded, as the -5 rating on your question indicates. (And for what it's worth, I didn't even downvote your question- although I think the wording is poor also). Instead of downvoting people who try to help you- as you seem to be doing- consider rewording your question. – bvaughn Mar 19 '15 at 05:21
-1

If you want a literal backslash in a string, you need to enter two backslashes. The first one escapes the second one, indicating that it should be interpreted as an actual backslash rather than an escape character.

If you want a string whose content is the following character:

\

you would use the following string literal:

"\\"
user2357112
  • 260,549
  • 28
  • 431
  • 505
  • You've not read or understood my question. I'm aware of this, I want to do so programmatically, given an input string containing escaped characters, generate the double escaped suitable for a new RegExp construction – Louis Maddox Mar 19 '15 at 02:29