Sure, the previous example could've also easily been replicated using a string method. The difference between normal search functions and regular expressions becomes much more evident when special characters (or metacharacters) are used in regular expressions. They provide a unique meaning to the expression when used. In this lesson, we will discuss some of the commonly used metacharacters.
The most basic among these is the use of square brackets ([ ]
) to define a character class. Any character inside the class is matched with the given string.
xxxxxxxxxx
let s = 'john479';
if (/[0-9]/.test(s))
console.log("Found a match!");
else
console.log("Did not find a match.");
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment