0

I have a string with several "\r\n" and I'm not sure how to replace it due to the several '\'. With normal '\r\n' I would do that

'some string \r\n' here'.replace(/\r\n/g, 'wtv');

But for this one I'm not sure:

// failed attempt below :(
'some string \\r\\n here'.replace('/\\r\\n/g', 'wtv);
Josh Lee
  • 171,072
  • 38
  • 269
  • 275
François Richard
  • 6,817
  • 10
  • 43
  • 78
  • 2
    `.replace(/\\r\\n/g, 'wtv')` - remove the single quotes to turn the string literal to the regex literal. There must be a closing single quote after `wtv`, too. – Wiktor Stribiżew Sep 12 '16 at 08:21
  • You might want to read up a little more on regular expressions. The backslash is a special character in regular expressions, just like in string literals. – Some programmer dude Sep 12 '16 at 08:22
  • 1
    See this [question] (http://stackoverflow.com/questions/20023625/javascript-replace-not-replacing-text-containing-literal-r-n-strings). It will help – Dharmesh Sep 12 '16 at 08:23
  • François, your code was ok, just the regex literal does not need to be put inside quotes. – Wiktor Stribiżew Sep 12 '16 at 08:39

1 Answers1

1

You can use following code :

console.log("some string \\r\\n here".replace(/(?:\\[rn])+/g, "wtv"));

above code will replace all '\r\n' with 'wtv' in your string

Abhijeet
  • 4,069
  • 1
  • 22
  • 38