Meta characters are special characters that have a symbolic meaning and are used to
define patterns.
1) Dot(.):
matches any single character except for newline characters.
2) Caret(^): matches the begining of a string or line.
3) Dollar sign ($): matches the end of a string or line.
4) Asterisk(*): matches zero or more occurences of the preceding character or group.
5) plus sign(+): matches one or more occurences of the preceding character or group.
6) question mark(?):
matches zero or one occurences of the preceding character or group.
7) square brackets([]):
i) [abc]: matches any single character in the set.
ii) [^abc]: mathes any single character that is not "a","b","c".
8) backslash():
i) \d: matches any digit character.
ii) \w: Matches any alphanumeric characters or underscore. /[a-zA-Z0-9_]/
iii) \s: Matches any whitespace character.
9) Pipe(|): a|b matches either "a" or "b".
10) parentheses ():
(abc): groups multiple characters or expressions together.
// Dot(.):
let regex = /h.t/;
console.log(regex.test("het"));
console.log(regex.test("heat"));
console.log(regex.test("hot"));
console.log(regex.test("hut"));
console.log(regex.test("hit"));
console.log(regex.test("hunt"));
// caret(^):
let regex = /^hello/;
console.log(regex.test("hello world"));
console.log(regex.test("Hello world"));
//dollar sign($):
let regex = /world$/;
console.log(regex.test("hello world"));
console.log(regex.test("world Hello"));
//asterisk(*):
let regex = /bo*k/;
console.log(regex.test("bk"));
console.log(regex.test("bok"));
console.log(regex.test("book"));
console.log(regex.test("bokkkk"));
console.log(regex.test("break"));
console.log(regex.test("boeaker"));
// plus sign(+):
let regex = /go+t/;
console.log(regex.test("gt"));
console.log(regex.test("got"));
console.log(regex.test("goot"));
console.log(regex.test("goat"));
console.log(regex.test("gooooooooooooooot"));
console.log(regex.test("ghost"));
// question mark(?):
let regex = /bo?k/;
console.log(regex.test("bk"));
console.log(regex.test("bok"));
console.log(regex.test("book"));
console.log(regex.test("bokkkk"));
console.log(regex.test("break"));
console.log(regex.test("boker"));
// square brackets[]:
let regex = /[a-z A-Z 0-9_]/;
console.log(regex.test("hellO 123"));
// backslash(\):
i)
let regex = /\d/;
console.log(regex.test("12345"));
console.log(regex.test("abcde"));
ii)
let regex = /\s/;
console.log(regex.test("123 45"));
console.log(regex.test("abcde"));
//pipe(|):
let regex = /hello|world/;
console.log(regex.test("hello there"));
console.log(regex.test("hi world"));
// parentheses():
let regex = /(ab)/;
console.log(regex.test("ababababab"));
console.log(regex.test("abcdefg"));
special sequence:
special sequence provide shorthand notations for commonly used patterns.
1) \d: matches any digit character.(equivalent to [0-9]).
let regex = /\d/;
console.log(regex.test("12345"));
console.log(regex.test("abcde"));
2) \D: matches any non-digit character.(equivalent to [^0-9]).
let regex = /\D/;
console.log(regex.test("12345"));
console.log(regex.test("abcde"));
3) \w: Matches any alphanumeric characters or underscore.
(equivalent to /[a-zA-Z0-9_]/)
let regex = /\w/;
console.log(regex.test("hellO 123"));
let regex = "hellO 123";
console.log(/\w/.test(regex));
4) \W: Matches any non word character.
(equivalent to /[^a-zA-Z0-9_]/)
let regex = /\W/;
console.log(regex.test("hellO123"));
5)\s: Matches any whitespace character.(space, tab, newline).
let regex = /\s/;
console.log(regex.test("123 45"));
console.log(regex.test(""));
6) \S: Matches any non - whitespace character.
let regex1 = /\S/;
console.log(regex1.test("123 45"));
console.log(regex1.test(""));
7) \b: Matches a word boundary
*/
let regex = "123 Hello";
console.log(/\bHello\b/.test(regex));
/*
8) \B: Matches a non - word boundary
*/
let regex1 = "123, Hello";
console.log(/\BHello\B/.test(regex1));

0 Comments