JavaScript Regular Expressions

JavaScript Regular Expressions


JavaScript Regular Expressions
A Regular Expression (RegEx) is an object that describes a sequence of characters used defining a search pattern. 
Creating a Regular Expression
You construct a Regular Expression in one of two ways:
  • Using a regular expression literal, which consists of a pattern enclose between slashes, as follows:
const re = /ab+c/;
Regular expression literals provide compilation of the regular expression when the script is loaded. If the regular expression remains constant, using this can improve performance.
  • Or calling the constructor function of the RegExp object, as follows :
Const re = new RegExp(‘ab+c’);
Regular Expression Cheat Sheet
Example 1 – Do a case-insensitive search for “webdesigninghouse” in a string:
Output - 
Example2 – Do a global search for “e” in a string:
Output -
Example 3 - Calling the constructor function of the RegExp object, as follows:
 
Output -
Before clicking the button:
After clicking the button: