What is a regular expression?
Popular Regex and String Methods
The test() method executes a search for a match between a regular expression and a specified string. The return is true or false.
const string = "This is an example of test"
const regex = /exam/;
console.log(regex.test(string)) =>> true
There is a method of String. It finds matches in a string and returns an array of the matches.
const string = "This is an example of the match method";
var regex =/ match/
console.log(str.match(regex)); ==> [ "match" ]
To find all matches, we use the g (global) flag(see below)
const string = "This is a test to test match method";
const regex =/test/g
console.log(str.match(regex)); ==>[ "test", "test" ]
In case of no matches,
null is returned (and NOT an empty array. Important to remember while applying array methods).
var string = "This is a test" ;
console.log(str.match(/hello/)); ==> null
Flags are optional parameters that can be added to a regular expression to affect its matching behavior. There are two main flags that modify the search in different ways
- i: Ignores casing (/e/i will match both ‘e’ and ‘E’)
- g: Global search returning all matches for a given expression inside a string — without it, only the first match is returned
These flags can be used separately or together in any order as seen down below.
conts string = "Hello, hello";
==> Array(2)[ "He","he"]
Groups and ranges
Several characters or character classes inside square brackets
[…] means we want to “search for any of these characters"
For example [bd] will match for ‘b’ or ‘d’
Character classes are shorthands for certain character sets.
\d = Any digit character (from 0 to 9)
\D = Non-digit: any character except \d
\w = Any alphanumeric character from the basic Latin alphabet (including. digit), including the underscore
\W = Non-wordly character: anything but \w.
\s = a single white space character, including space, tab, form feed, line feed, and other Unicode spaces
\S = Non-space: any character except \s, for instance a letter