Matching double backslashes in a string requires two escape backslashes. But event that doesn't match in native JavaScript functions as can be seen below:
const str = 'sj\\sf\sd'
str.match(/\\\\/g); /*null*/
str.indexOf('\\\\'); /*-1*/
str.replace(/\\\\/, '') /*'sj\sfsd'*/ /*<--wrong characters replaced*/
Whereas String.raw
works:
const str = String.raw`sj\\sf\sd`
str.match(/\\\\/g); /*['\\']*/
str.indexOf('\\\\'); /*2*/
str.replace(String.raw`\\`, '') /*'sjsf\sd'*/
Similar questions have been asked about this topic but none explain the reason behind this quirkiness: