Character Escapes


There are many characters that cannot be represented in literal form easily in a pattern. For example, a new line, a tab, a carriage return, a verticle tab etc. There is no easy way to type them in a text string or a regex pattern. Character escape is the way you can represent hard to type characters in their literal forms. A character escape is specified by a backslash (\) followed by a letter. For example, a new line is specified by \n. A tab is specified by \t, a form feed by \f, a vertical tab by \v etc

If you want a match for
NH
563
(NH followed by new line followed by 563), your pattern will be /NH\n563/

let pattern = /NH\n563/dg;
let input = `The NH
563 is 1000 km long`;
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-------------
NH
563
index = 4
input = The NH
563 is 1000 km long
indices = (4,10)

List Of Character Escapes

Character EscapeMatches
\tTab
\nNewline
\rCarriage Return
\vVertical Tab
\0Null Character
\cXMatches a control character using caret notation, where ‘X’ is a letter from A-Z or a-z (corresponding to code points U+0001-U+001A). For example, /\cJ/ matches ‘\n’
\xHHMatches the character with the code HH (two hexadecimal digits)
\uHHHHMatches a UTF-16 code-unit with the value HHHH (four hexadecimal digits)
\u{H…H}(Only when the u flag is set) Matches the character with the Unicode value U+H…H (1 to 6 hexadecimal digits)

Lets look at an example to match the null character with \0. The pattern \/here\0|there\0|Everywhere\0/dg matches either ‘here followed by the null character’, ‘there followed by the null character’ or ‘Everywhere followed by null character’

let pattern = /here\0|there\0|Everywhere\0/dg;
let input = 'Null here\0Null there\0Null Everywhere\0';
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-------------
here
index = 5
input = Null hereNull thereNull Everywhere
indices = (5,10)

----------Match 2-------------
there
index = 15
input = Null hereNull thereNull Everywhere
indices = (15,21)

----------Match 3-------------
Everywhere
index = 26
input = Null hereNull thereNull Everywhere
indices = (26,37)

Let us try the \c escape character \cJ which is a new line. The pattern /H\cJ5/ matches H followed by new line followed by 5

let pattern = /H\cJ5/dg;
let input = `NH
563
NH
503
`;
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-------------
H
5
index = 1
input = NH
563
NH
503
indices = (1,4)

----------Match 2-------------
H
5
index = 8
input = NH
563
NH
503
indices = (8,11)

The hex codes for d, a and b are 64, 61 and 62. If you have to match dab, you use /\x64\x61\x62/

let pattern = /\x64\x61\x62/dg;
let input = 'abracadabra abracadabra';
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-------------
dab
index = 6
input = abracadabra abracadabra
indices = (6,9)

----------Match 2-------------
dab
index = 18
input = abracadabra abracadabra
indices = (18,21)