This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Smartphone icons created by Freepik - Flaticon
7.6 JS RegExp
- A regular expression is a sequence of characters that forms a search pattern.
- The search pattern can be used for text search and text replace operations.
What Is a Regular Expression?
- A regular expression is a sequence of characters that forms a search pattern.
- When you search for data in a text, you can use this search pattern to describe what you are searching for.
- A regular expression can be a single character, or a more complicated pattern.
- Regular expressions can be used to perform all types of text search and text replace operations.
Syntax
/pattern/modifiers;
Example
var patt = /w3schools/i;
- Example explained:
- /w3schools/i is a regular expression.
- w3schools is a pattern (to be used in a search).
- i is a modifier (modifies the search to be case-insensitive).
Using String Methods
- In JavaScript, regular expressions are often used with the two string methods: search() and replace().
- The search() method uses an expression to search for a match, and returns the position of the match.
- The replace() method returns a modified string where the pattern is replaced.
Using String search() With a String
The search() method searches a string for a specified value and returns the position of the match:
Use a string to do a search for "W3schools" in a string:
var str = "Visit W3Schools!";
var n = str.search("W3Schools");
Using String search() With a Regular Expression
Use a regular expression to do a case-insensitive search for "w3schools" in a string:
var str = "Visit W3Schools";
var n = str.search(/w3schools/i);
Using String replace() With a String
The replace() method replaces a specified value with another value in a string:
var str = "Visit Microsoft!";
var res = str.replace("Microsoft", "W3Schools");
Try it yourself.
Use String replace() With a Regular Expression
Use a case insensitive regular expression to replace Microsoft with W3Schools in a string:
var str = "Visit Microsoft!";
var res = str.replace(/microsoft/i, "W3Schools");
Did You Notice?
- Regular expression arguments (instead of string arguments) can be used in the methods above.
- Regular expressions can make your search much more powerful (case insensitive for example).
Regular Expression Modifiers
Modifiers can be used to perform case-insensitive more global searches:
Modifier |
Description |
Try it |
i |
Perform case-insensitive matching |
Try it |
g |
Perform a global match (find all matches rather than stopping after the first match) |
Try it |
m |
Perform multiline matching |
Try it |
Regular Expression Patterns
Brackets are used to find a range of characters:
Expression |
Description |
Try it |
[abc] |
Find any of the characters between the brackets |
Try it |
[0-9] |
Find any of the digits between the brackets |
Try it |
(x|y) |
Find any of the alternatives separated with | |
Try it |
Metacharacters are characters with a special meaning:
Metacharacter |
Description |
Try it |
\d |
Find a digit |
Try it |
\s |
Find a whitespace character |
Try it |
\b |
Find a match at the beginning of a word like this: \bWORD, or at the end of a word like this: WORD\b |
Try it Try it |
\uxxxx |
Find the Unicode character specified by the hexadecimal number xxxx |
Try it |
Quantifiers define quantities:
Quantifier |
Description |
Try it |
n+ |
Matches any string that contains at least one n |
Try it |
n* |
Matches any string that contains zero or more occurrences of n |
Try it |
n? |
Matches any string that contains zero or one occurrences of n |
Try it |
Using the RegExp Object
Using the RegExp Object
Using the RegExp Object
- The test() method is a RegExp expression method.
- It searches a string for a pattern, and returns true or false, depending on the result.
- The following example searches a string for the character "e":
var patt = /e/;
patt.test("The best things in life are free!");
You don't have to put the regular expression in a variable first. The two lines above can be shortened to one:
/e/.test("The best things in life are free!");
Using exec()
- The exec() method is a RegExp expression method.
- It searches a string for a specified pattern, and returns the found text as an object.
- If no match is found, it returns an empty (null) object.
- The following example searches a string for the character "e":
/e/.exec("The best things in life are free!");
Complete RegExp Reference
JavaScript icons used in the buttons provided by ICONS8.COM. Smartphone icons created by Freepik - Flaticon
Example files created in this module:
JS Sets Map Objects part 1
JS Sets Map Objects part 2
JS Sets Map Objects part 3
JS Sets Map Objects part 4
JS Sets Map Objects part 5
JS Sets Delete Map Objects
JS Sets Map.has() part 1
JS Sets Map.has() part 2
JS Sets Map Objects forEach()
JS Sets Map Objects Map.entries()
JS Map Objects
JS Map Objects - Map.set() part 1
JS Map Objects - Map.set() part 2
JS Map Objects Map.get()
JS Map Objects - Map.size
JS Map Objects - Map.delete()
JS Map Objects - Map.has() part 1
JS Map Objects - Map.has() part 2
JS Map Objects - Map.forEach()
JS Map Objects - Map.entries()
JS typeof Operator part 1
JS typeof Operator part 2
JS typeof Operator part 3
JS typeof Properties - constructor Property part 1
JS typeof Properties - constructor Property part 2
JS typeof Properties - Array Object
JS typeof Operator - Date Object
JS typeof Operator - Date Function
JS typeof Operator - undefined part 1
JS typeof Operator - undefined part 2
JS typeof Operator - empty values
JS typeof Operator - null
JS typeof Operator - undefined Data Type
JS instanceof Operator
JS void Operator
JS Operators - typeof Operator
JS Properties - constructor Property
JS Arrays - isArray() function part 1
JS Arrays - isArray() function part 2
JS Date Object - isDate() function part 1
JS Date Object - isDate() function part 2
JS Converting Numbers to Strings part 1
JS Converting Numbers to Strings part 2
JS typeof Operator part 1
JS typeof Operator part 2
JS Bitwise AND
JS Bitwise OR
JS Bitwise XOR
JS Bitwise NOT
JS Bitwise Left
JS Bitwise Right
JS Unsigned Bitwise Right
JS Convert Decimal to Binary
JS Convert Binary to Decimal
JS String Methods
JS Regular Expressions part 1
JS String Methods - String replace()
JS Regular Expressions part 2
JS Regular Expressions part 3
JS Error Handling part 1
JS try catch
JS Errors - RangeError
JS Errors - ReferenceError
JS Errors - SyntaxError
JS Errors - TypeError
JS Errors - URIError
JS Scope - Local Variables
JS Scope - GLOBAL Variable
JS Global Variables
JS Scope - Global HTML variables
JS Hoisted Declarations part 1
JS Hoisted Declarations part 2
JS Hoisting part 1
JS Hoisting part 2
JS Initializations are Not Hoisted part 1
JS Initializations are Not Hoisted part 2
JS Initializations are Not Hoisted part 3
JS "use strict" Mode part 1
JS "use strict" Mode part 2
JS this Keyword
JS this Keyword [object Window] part 1
JS this Keyword - window object
JS this Keyword [object Window] part 2
JS this Keyword - Function
JS this Keyword - Event Handler
JS this Keyword - Event Handler
JS this Keyword - person object part 1
JS this Keyword - person object part 2
JS this Keyword - Explicit Function Binding
JS this Keyword - Function bind()
JS this Keyword - Arrow Function part 1
JS this Keyword - Arrow Function part 2