1

Is there anything close to an r'string' in javascript such as exists in some other languages, such as python? For example, I am trying to do the following, which is a copy-paste regex from another program that supports r-strings to not have to double-escape the \'s to test that various sequences are working in a RE2 regex:

import RE2 from 'RE2';

let re = new RE2("\d\D\s\S\w\W\a\f\t\n\r\v\#\122\x12\x{12}\Cx\Q^\E\A\b\B\z[[:alnum:]]\pC\PC\p{Cc}\P{Cc}\p{Greek}a?a*a+a|{}<>:,-.$^\]\[\(\)");
let res = re.exec("x");
console.log(res);

Or is the best way to just do a find-and-replace from the code editor?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
David542
  • 104,438
  • 178
  • 489
  • 842

1 Answers1

2

yes, this is called "raw strings": String.raw

r = String.raw`\a\n\r\x`

console.log(r)
gog
  • 10,367
  • 2
  • 24
  • 38
  • 1
    ... and if you need this often or you want to feel more like Python for some reason, you can assign the `String.raw` function to a variable `r`: `const r = String.raw` - then you can write `const myString = r\`C:\Users\me\``. Still requires backticks and not quotes though. – CherryDT Oct 02 '22 at 21:37