There are a number of special characters called meta characters like ., *, +, ?, ^, $, (, ), [, ], {, }, |, \ that have special meaning in regular expressions. If you want to match them literally, you must escape them with a backslash () like \$, \* etc. To include a backslash character literally in a regular expression, you must escape it with another backslash (/\\/).
let pattern = /ab\\cd/gd; let input = "xyzab\\cdpqr";let matches = input.matchAll(pattern);let i = 1;for (let match of matches) { console.log(`----------Match ${i}-------------`); for (text of match) { console.log(text); } let index = match.index; console.log("index = " + index); let inputStr = match.input; console.log("input = " + inputStr); let indices = match.indices; for (let [first, last] of indices) { console.log("indices = (" + first + "," + last + ")"); } console.log("\n"); i ++;}
Output:
----------Match 1-------------
ab\cd
index = 3
input = xyzab\cdpqr
indices = (3,8)