Regular Expressions

What are Regular Expressions?

Regular expressions are patterns used to match character combinations in strings.

Creating a Regular Expression

You construct a regular expression in one of two ways:

  • Using a regular expression literal, which consists of a pattern enclosed between slashes, as follows:
    									
    										const re = /ab+c/;
    									
    								
  • Or calling the constructor function of the RegExp object, as follows:
    									
    										const re = new RegExp('ab+c');
    									
    								

Where can we use regular Expressions?

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.

Regular Expression Modifiers

Syntax
						
						  /pattern/modifiers;
						
					
Modifiers
  • i - perform case-insensitive matching
  • g - perform a global match (find all matches rather than stopping after the first match)
  • m - perform multiline matching

Examples

						
// без флага 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 groups

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
						
					

Character groups

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
						
					

Character groups

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

Meta-characters are characters with a special meaning. There are many meta character but I am going to cover the most important ones here.

  • \d — Match any digit character ( same as [0-9] )
  • \w — Match any word character. A word character is any letter, digit, and underscore. (Same as [a-zA-Z0–9_] )
  • \s — Match a whitespace character (spaces, tabs etc)
  • \t — Match a tab character only
  • \D — Match any non digit character (same as [^0–9])
  • \W — Match any non word character (Same as [^a-zA-Z0–9_] )
  • \S — Match a non whitespace character

Quantifiers

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
						
					

Quantifiers

* —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
						
					

Quantifiers

? — 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
						
					

Quantifiers

^ — 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
						
					

Quantifiers

$ — 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
						
					

Quantifiers

{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
						
					

Quantifiers

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
						
					
Thank for your attention!