- JavaScript Basics
- JS Home
- JS Syntax
- JS Placements
- JS Output
- JS Statements
- JS Keywords
- JS Comments
- JS Variables
- JS var
- JS let
- JS const
- JS var Vs let Vs const
- JS Operators
- JS Arithmetic Operators
- JS Assignment Operators
- JS Comparison Operators
- JS Logical Operators
- JS Bitwise Operators
- JS Ternary Operator
- JS Operator Precedence
- JS Data Types
- JS typeof
- JS Conditional Statements
- JS Conditional Statement
- JS if Statement
- JS if...else Statement
- JS switch Statement
- JS Loops
- JS for Loop
- JS while Loop
- JS do...while Loop
- JS break Statement
- JS continue Statement
- JS break Vs. continue
- JavaScript Popup Boxes
- JS Dialog Box
- JS alert Box
- JS confirm Box
- JS prompt Box
- JavaScript Functions
- JS Functions
- JS setTimeout() Method
- JS setInterval() Method
- JavaScript Events
- JS Events
- JS onclick Event
- JS onload Event
- JS Mouse Events
- JS onreset Event
- JS onsubmit Event
- JavaScript Arrays
- JS Array
- JS Find Length of Array
- JS Add Elements at Beginning
- JS Add Element at End
- JS Remove First Element
- JS Remove Last Element
- JS Get First Index
- JS Get Last Index
- JS Reverse an Array
- JS Sort an Array
- JS Concatenate Arrays
- JS join()
- JS toString()
- JS from()
- JS Check if Value Exists
- JS Check if Array
- JS Slice an Array
- JS splice()
- JS find()
- JS findIndex()
- JS entries()
- JS every()
- JS fill()
- JS filter()
- JS forEach()
- JS map()
- JavaScript Strings
- JS String
- JS Length of String
- JS Convert to Lowercase
- JS Convert to Uppercase
- JS String Concatenation
- JS search()
- JS indexOf()
- JS search() Vs. indexOf()
- JS match()
- JS match() Vs. search()
- JS replace()
- JS toString()
- JS String()
- JS includes()
- JS substr()
- JS Slice String
- JS charAt()
- JS repeat()
- JS split()
- JS charCodeAt()
- JS fromCharCode()
- JS startsWith()
- JS endsWith()
- JS trim()
- JS lastIndexOf()
- JavaScript Objects
- JS Objects
- JS Boolean Object
- JavaScript Math/Number
- JS Math Object
- JS Math.abs()
- JS Math.max()
- JS Math.min()
- JS Math.pow()
- JS Math.sqrt()
- JS Math.cbrt()
- JS Math.round()
- JS Math.ceil()
- JS Math.floor()
- JS Math.trunc
- JS toFixed()
- JS toPrecision()
- JS Math.random()
- JS Math.sign()
- JS Number.isInteger()
- JS NaN
- JS Number()
- JS parseInt()
- JS parseFloat()
- JavaScript Date and Time
- JS Date and Time
- JS Date()
- JS getFullYear()
- JS getMonth()
- JS getDate()
- JS getDay()
- JS getHours()
- JS getMinutes()
- JS getSeconds()
- JS getMilliseconds()
- JS getTime()
- JS getUTCFullYear()
- JS getUTCMonth()
- JS getUTCDate()
- JS getUTCDay()
- JS getUTCHours()
- JS getUTCMinutes()
- JS getUTCSeconds()
- JS getUTCMilliseconds()
- JS toDateString()
- JS toLocaleDateString()
- JS toLocaleTimeString()
- JS toLocaleString()
- JS toUTCString()
- JS getTimezoneOffset()
- JS toISOString()
- JavaScript Browser Objects
- JS Browser Objects
- JS Window Object
- JS Navigator Object
- JS History Object
- JS Screen Object
- JS Location Object
- JavaScript Document Object
- JS Document Object Collection
- JS Document Object Properties
- JS Document Object Methods
- JS Document Object with Forms
- JavaScript DOM
- JS DOM
- JS DOM Nodes
- JS DOM Levels
- JS DOM Interfaces
- JavaScript Cookies
- JS Cookies
- JS Create/Delete Cookies
- JavaScript Regular Expression
- JS Regular Expression
- JS RegEx .
- JS RegEx \w and \W
- JS RegEx \d and \D
- JS RegEx \s and \S
- JS RegEx \b and \B
- JS RegEx \0
- JS RegEx \n
- JS RegEx \xxx
- JS RegEx \xdd
- JS RegEx Quantifiers
- JS RegEx test()
- JS RegEx lastIndex
- JS RegEx source
- JavaScript Advance
- JS Page Redirection
- JS Form Validation
- JS Validations
- JS Error Handling
- JS Exception Handling
- JS try-catch throw finally
- JS onerror Event
- JS Multimedia
- JS Animation
- JS Image Map
- JS Debugging
- JS Browser Detection
- JS Security
- JavaScript Misc
- JS innerHTML
- JS getElementById()
- JS getElementsByClassName()
- JS getElementsByName()
- JS getElementsByTagName()
- JS querySelector()
- JS querySelectorAll()
- JS document.write()
- JS console.log()
- JS instanceof
- JavaScript Programs
- JavaScript Programs
JavaScript Regular Expression
This post is published to provide the complete information about regular expression in JavaScript.
What is a Regular Expression?
A regular expression is something like a sequence of characters. But it is not a normal sequence of characters, rather it is a sequence that forms a search pattern.
Sometime, we need to implement a simple or complex search in our application, where the search is based on some pattern. For example: let us suppose, we are going to implement a form that asks some information about the user like:
- password
- contact numbers
- and many more
where we need to match the pattern of these data before feeding into the database. For example, if we take the first data, that is, email, then we need to match the entered email by user properly. For example, an email must be started with a unique username like fresherearth followed by @ along with a company URL say gmail.com, hotmail.com etc.
So these are some of the scenario where regular expression comes into picture, that allows to implement the match pattern to any data we need to match. But before starting the match pattern, we need to understand its modifiers and patterns. So let's get started.
JavaScript Regular Expression Syntax
Whatever the pattern or regular expression you use for matching, but the basic and general form you need to follow is:
/pattern/modifiers;
Here pattern refers to the search pattern that is used to match. For example:
<!DOCTYPE html> <html> <body> <p>The first 'is' is available at Index no.<span id="xyz"></span></p> <script> let myString = "JavaScript is Fun, is not it?"; let pos = myString.search(/is/); document.getElementById("xyz").innerHTML = pos; </script> </body> </html>
The first 'is' is available at Index no.
Note: The search() method is used to search a substring (value) in a string using regular expression.
And modifiers are used to change the default search method. For example:
<!DOCTYPE html> <html> <body> <p>The first 'is' available at Index no.<span id="xyz"></span></p> <script> let myString = "JavaScript Is Fun. Is not it?"; let pos = myString.search(/is/i); document.getElementById("xyz").innerHTML = pos; </script> </body> </html>
The first 'is' available at Index no.
The i after /is/ force the search pattern to be go case-insensitive.
JavaScript Regular Expression Flags/Modifiers
Flags or modifiers in regular expression are used to modify the search method. Here are the list of flags that can be used in JavaScript regular expression:
- g - Used to force the search pattern to go for all matches
- i - Used to force the search pattern to go case-insensitive matching
- m - Used to force the match pattern to go multiline
For example, let me create a JavaScript example without using any modifiers. Then will use modifier(s) to see the change:
<!DOCTYPE html> <html> <body> <p id="xyz"></p> <script> let myString = "JavaScript is Fun, is not it?"; let matches = myString.match(/is/); document.getElementById("xyz").innerHTML = matches; </script> </body> </html>
Note: The match() method is used to match a string with/using a regular expression.
Now let me implement the g modifier to allow the match pattern globally. That is, to get all matches, instead of stopping after the first match:
<!DOCTYPE html> <html> <body> <p id="xyz"></p> <script> let myString = "JavaScript is Fun, is not it?"; let matches = myString.match(/is/g); document.getElementById("xyz").innerHTML = matches; </script> </body> </html>
Then let's use i modifier to go case-insensitive matching:
<!DOCTYPE html> <html> <body> <p id="xyz"></p> <script> let myString = "JavaScript is Fun. Is not it?"; let matches = myString.match(/is/gi); document.getElementById("xyz").innerHTML = matches; </script> </body> </html>
JavaScript Regular Expression Patterns
In JavaScript regular expression, we can use brackets to apply the match pattern through range of characters defined in the brackets. For example:
- [abc] - Matches any of the enclosed characters. That is, this pattern can be used to match any characters that are in the brackets. We can also use [a-c].
- [^abc] - Matches any character except the enclosed characters. We can also use [^a-c].
- [0-9] - Matches any of the enclosed numbers.
- [^0-9] - Matches any character except the enclosed characters.
- (a|b) - Matches any of the character.
For example:
<!DOCTYPE html> <html> <body> <p>List of all Matches for First Pattern: <b><span id="resA"></span></b></p> <p>List of all Matches for Second Pattern: <b><span id="resB"></span></b></p> <p>List of all Matches for Third Pattern: <b><span id="resC"></span></b></p> <script> let text = "WE have created jobails.com to provide FREE online learning."; let a = text.match(/[re]/gi); let b = text.match(/[^re]/gi); let c = text.match(/[a-d]/gi); document.getElementById("resA").innerHTML = a; document.getElementById("resB").innerHTML = b; document.getElementById("resC").innerHTML = c; </script> </body> </html>
List of all Matches for First Pattern:
List of all Matches for Second Pattern:
List of all Matches for Third Pattern:
Let me create another example that uses the [0-9] and [^0-9] pattern:
<!DOCTYPE html> <html> <body> <p>List of all Matches for First Pattern: <b><span id="A"></span></b></p> <p>List of all Matches for Second Pattern: <b><span id="B"></span></b></p> <p>List of all Matches for Third Pattern: <b><span id="C"></span></b></p> <script> let text = "X23sx495682043-234932lo6023"; let a = text.match(/[0-9]/g); let b = text.match(/[^0-9]/g); let c = text.match(/[3-6]/g); document.getElementById("A").innerHTML = a; document.getElementById("B").innerHTML = b; document.getElementById("C").innerHTML = c; </script> </body> </html>
List of all Matches for First Pattern:
List of all Matches for Second Pattern:
List of all Matches for Third Pattern:
Here is another example regarding (a|b) pattern:
<!DOCTYPE html> <html> <body> <p id="xyz"></p> <script> let mystr = "What do you like? Cricket or Football?"; let ptrn = /(cricket|football)/gi; document.getElementById("xyz").innerHTML = mystr.match(ptrn); </script> </body> </html>
It is not limited to use only two character or words. We can use multiple words or characters to match, from the listed characters or words. For example:
<!DOCTYPE html> <html> <body> <p id="xyz"></p> <script> let mystr = "London, Berlin, london, Austin, Dallas, Seattle"; let ptrn = /(London|Austin|Dallas|Berlin)/gi; document.getElementById("xyz").innerHTML = mystr.match(ptrn); </script> </body> </html>
List of Metacharacters used in JavaScript Regular Expression
Metacharacters are characters that starts with a backslash (/) followed by character(s) that has/have special meaning in computer programming and plays an important role in defining the match pattern while working with regular expression in JavaScript.
Here are the list of metacharacters used in JavaScript regular expression:
- . - used to match any character (except newline and/or other line terminators).
- \w - used to match all word characters. A-Z, a-z, 0-9 and, underscore (_) are all word characters.
- \W - used to match all characters except word characters.
- \d - used to match any digit from 0 to 9.
- \D - used to match any character other than digits (0-9).
- \s - used to match any whitespace. A space, tab, vertical tab, new line, carriage return, and form feed are all whitespaces.
- \S - used to match any character other than whitespace characters.
- \b - used to match any word beginning or ending with specified character or combination of characters.
- \B - used to match any word that does not begins or ends with specified character or combination of characters.
- \0 - used to find a null character.
- \n - used to match new line in the specified string.
- \f - Used to match form feed character. Since it is exactly similar to matching of new line character. Therefore I have not created a separate article on it.
- \r - Used to match carriage return character. It is also similar to \n
- \t - Used to match tab character. Similar to \n
- \v - Used to match vertical tab character. Similar to \n
- \xxx - used to match a character using its octal number, in the form xxx. For example, 165, which is equal to 'u'
- \xdd - used to match a character using its hexadecimal number, in the form xdd. For example, 75, which is equal to 'u'
The description and examples of above metacharacters are provided in its separate tutorial.
Quantifiers in JavaScript regular expression are described in its separate tutorial.
« Previous Tutorial Next Tutorial »