- JavaScript Introduction
 - JavaScript Where To
 - JavaScript Output
 - JavaScript Statements
 - JavaScript Syntax
 - JavaScript Comments
 - JavaScript Variables
 - JavaScript Let
 - JavaScript Const
 - JavaScript Operators
 - JavaScript Assignment
 - JavaScript Data Types
 - JavaScript Functions
 - JavaScript Objects
 - JavaScript Events
 - JavaScript Strings
 - JavaScript String Methods
 - JavaScript Numbers
 - JavaScript Number Methods
 - JavaScript Arrays
 - JavaScript Array Const
 - JavaScript Array Methods
 - JavaScript Sorting Arrays
 - JavaScript Array Iteration
 - JavaScript Date Objects
 - JavaScript Date Formats
 - JavaScript Get Date Methods
 - JavaScript Set Date Methods
 - JavaScript Math Object
 - JavaScript Random
 - JavaScript Booleans
 - JavaScript Comparison And Logical Operators
 - JavaScript If Else And Else If
 - JavaScript Switch Statement
 - JavaScript For Loop
 - JavaScript Break And Continue
 - JavaScript Type Conversion
 - JavaScript Bitwise Operations
 - JavaScript Regular Expressions
 - JavaScript Errors
 - JavaScript Scope
 - JavaScript Hoisting
 - JavaScript Use Strict
 - The JavaScript This Keyword
 - JavaScript Arrow Function
 - JavaScript Classes
 - JavaScript JSON
 - JavaScript Debugging
 - JavaScript Style Guide
 - JavaScript Common Mistakes
 - JavaScript Performance
 - JavaScript Reserved Words
 - JavaScript Versions
 - JavaScript History
 - JavaScript Forms
 - JavaScript Validation API
 - JavaScript Objects
 - JavaScript Object Properties
 - JavaScript Function Definitions
 - JavaScript Function Parameters
 - JavaScript Function Invocation
 - JavaScript Closures
 - JavaScript Classes
 - Java Script Async
 - JavaScript HTML DOM
 - The Browser Object Model
 - JS Ajax
 - JavaScript JSON
 - JavaScript Web APIs
 - JS Vs JQuery
 
JavaScript Regular Expressions
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 = /Mytat/i;Example explained:
/Mytat/i is a regular expression.
Mytat 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:
Example
Use a string to do a search for "Mytat" in a string:var n = str.search("Mytat");
Using String search() With a Regular Expression
Example
Use a regular expression to do a case-insensitive search for "mytat" in a string:
var n = str.search(/mytat/i);
The result in n will be:
6
Using String replace() With a String
The replace() method replaces a specified value with another value in a string:
var res = str.replace("Microsoft", "Mytat");
Use String replace() With a Regular Expression
Example
Use a case insensitive regular expression to replace Microsoft with mytat in a string:
var res = str.replace(/microsoft/i, "Mytat");
The result in res will be:
Visit Mytat!
Regular Expression Modifiers
Modifiers can be used to perform case-insensitive more global searches:
| Modifier | Description | 
|---|---|
| i | Perform case-insensitive matching | 
| g | Perform a global match (find all matches rather than stopping after the first match) | 
| m | Perform multiline matching | 
Regular Expression Patterns
Brackets are used to find a range of characters:
| Expression | Description | 
|---|---|
| [abc] | Find any of the characters between the brackets | 
| [0-9] | Find any of the digits between the brackets | 
| (x|y) | Find any of the alternatives separated with | | 
Metacharacters are characters with a special meaning:
| Metacharacter | Description | 
|---|---|
| \d | Find a digit | 
| \s | Find a whitespace character | 
| \b | Find a match at the beginning of a word like this: \bWORD, or at the end of a word like this: WORD\b | 
| \uxxxx | Find the Unicode character specified by the hexadecimal number xxxx | 
Quantifiers define quantities:
| Quantifier | Description | 
|---|---|
| n+ | Matches any string that contains at least one n | 
| n* | Matches any string that contains zero or more occurrences of n | 
| n? | Matches any string that contains zero or one occurrences of n | 
Practice Excercise Practice now
Using The RegExp Object
In JavaScript, the RegExp object is a regular expression object with predefined properties and methods.
Using test()
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":
Example
patt.test("The best things in life are free!");
Since there is an "e" in the string, the output of the code above will be:
true
You don't have to put the regular expression in a variable first. The two lines above can be shortened to one:
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":
Example 1
Practice Excercise Practice now
Products
Partner
Copyright © RVR Innovations LLP 2025 | All rights reserved - Mytat.co is the venture of RVR Innovations LLP