Regular expressions are patterns used to match character combinations in strings.
You construct a regular expression in one of two ways:
const re = /ab+c/;
const re = new RegExp('ab+c');
In JavaScript, regular expressions are also objects.
These patterns are used with the exec() and test() methods of RegExp, and with the match(), matchAll(), replace(), replaceAll(), search() and split() methods of String.
/pattern/modifiers;
Modifiers
// без флага g
alert( "We will, we will".replace(/we/i, "I") ); // I will, we will
// с флагом g
alert( "We will, we will".replace(/we/ig, "I") ); // I will, I will
Character set [xyz] — A character set is a way to match different characters in a single position, it matches any single character in the string from characters present inside the brackets. For example:
let regex = /[bt]ear/;
console.log(regex.test('tear'));
// returns true
console.log(regex.test('bear'));
// return true
console.log(regex.test('fear'));
// return false
Negated character set [^xyz] — It matches anything that is not enclosed in the brackets. For example:
let regex = /[^bt]ear/;
console.log(regex.test('tear'));
// returns false
console.log(regex.test('bear'));
// return false
console.log(regex.test('fear'));
// return true
Ranges [a-z] — Suppose we want to match all of the letters of an alphabet in a single position, we could write all the letters inside the brackets, but there is an easier way and that is ranges. For example: [a-h] will match all the letters from a to h. Ranges can also be digits like [0-9] or capital letters like [A-Z].
let regex = /[a-z]ear/;
console.log(regex.test('fear'));
// returns true
console.log(regex.test('tear'));
// returns true
Meta-characters are characters with a special meaning. There are many meta character but I am going to cover the most important ones here.
Quantifiers are symbols that have a special meaning in a regular expression.
+ — Matches the preceding expression 1 or more times.
var regex = /\d+/;
console.log(regex.test('8'));
// true
console.log(regex.test('88899'));
// true
console.log(regex.test('8888845'));
// true
* —Matches the preceding expression 0 or more times.
let regex = /go*d/;
console.log(regex.test('gd'));
// true
console.log(regex.test('god'));
// true
console.log(regex.test('good'));
// true
? — Matches the preceding expression 0 or 1 time, that is preceding pattern is optional.
let regex = /goo?d/;
console.log(regex.test('god'));
// true
console.log(regex.test('good'));
// true
console.log(regex.test('goood'));
// false
^ — Matches the beginning of the string, the regular expression that follows it should be at the start of the test string. i.e the caret (^) matches the start of string.
let regex = /^g/;
console.log(regex.test('good'));
// true
console.log(regex.test('bad'));
// false
$ — Matches the end of the string, that is the regular expression that precedes it should be at the end of the test string. The dollar ($) sign matches the end of the string
let regex = /.com$/;
console.log(regex.test('test@testmail.com'));
// true
console.log(regex.test('test@testmail'));
// false
{N} — Matches exactly N occurrences of the preceding regular expression.
{N,} — Matches at least N occurrences of the preceding regular expression.
{N,M} — Matches at least N occurrences and at most M occurrences of the preceding regular expression (where M > N)
let regex = /go{2}d/;
console.log(regex.test('good'));
// true
let regex2 = /go{2,}d/;
console.log(regex.test('good'));
// true
let regex3 = /go{1,2}d/;
console.log(regex.test('goood'));
// false
Alternation X|Y — Matches either X or Y. For example:
let regex = /(green|red) apple/;
console.log(regex.test('green apple'));
// true
console.log(regex.test('red apple'));
// true
console.log(regex.test('blue apple'));
// false