Loading lesson path
A Regular Expression is a sequence of characters that forms a search pattern. Regex is a common shorthand for a regular expression.
Object for handling Regular Expressions.
Formula
Do a case - insensitive search for "w3schools" in a string:let text = "Visit W3Schools";
let n = text.search(/w3schools/i);Formula
/w3schools/i is a regular expression.w3schools is a pattern (to be used in a search).
Formula
i is a modifier (modifies the search to be case - insensitive)./ pattern / modifier flags
;Regular expressions are often used with the string methods
regex )
regex )
regex )
Search for "W3schools" in a string:
let text = "Visit W3Schools";
let n = text.match(/W3schools/);Replace Microsoft with W3Schools in a string:
let text = "Visit Microsoft!";
let result = text.replace(/Microsoft/i, "W3Schools");Search for "W3Schools" in a string:
let text = "Visit W3Schools";
let n = text.search(/W3Schools/);
RexExp Alternation (OR)In a regular expression an alternation is denoted with a vertical line character |. An alternation matches any of the alternatives separated with |.
A global search for the alternatives (red|green|blue):
let text = "Black, white, red, green, blue, yellow.";
let result = text.match(/red|green|blue/g);/pattern/ flags
Regular expression flags are parameters that can modify how a pattern is used, such as making it case-insensitive or global./g Performs a global match (find all) /i
/u Enables Unicode support (new 2015)
Formula
The /g Flag (Global)