[Solved] How can I store a string which contains backslashes (without manually double escaping)?


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.

2

solved How can I store a string which contains backslashes (without manually double escaping)?