Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
JavaScript RegExp ?! Quantifier
Next article icon

JavaScript RegExp Reference

Last Updated : 05 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

RegExp stands for Regular Expression. A regular expression is a sequence of characters that forms a search pattern. The search pattern can be used for text search and text to replace operations. A regular expression can be a single character or a more complicated pattern.

Syntax:

new RegExp("(Regular Expressioncharactersnonword)")

Example: This example searches the words whose starting character is “A” and ending character is “C” with only one character in between them.

JavaScript
let str = "ABC, A3C, A C, AXXCC!"; let regex = /A.C/g; console.log(str.match(regex)); 

Output
[ 'ABC', 'A3C', 'A C' ] 

The complete list of JavaScript RegExp is listed below:

JavaScript RegExp Constructor:

In JavaScript, a constructor gets called when an object is created using the new keyword.

RegExp(): It creates a new RegExp object.

JavaScript
const str = "geeksforgeeks" const reg = new RegExp(/[g]/, 'g'); console.log(str.match(reg)); 

Output
[ 'g', 'g' ] 

JavaScript RegExp Properties: A JavaScript property is a member of an object that associates a key with a value.

  • Instance Property: An instance property is a property that has a new copy for every new instance of the class.
Instance PropertiesDescription
constructorReturn the RegExp constructor function for the object.
dotAllIt checks if the ‘s’ flag is used or not in the Regular Expression
flagsThis property returns the flag being used in the current RegExp object
globalIt specifies whether the ‘g’ flag is used or not
hasIndicesIt specifies whether the ‘d’ flag is used or not
ignoreCaseIt specifies whether the ‘i’ flag is used or not
multilineIt specifies whether ‘m’ flag is set or not
sourceIt returns the text of the RegExp object 
stickyIt specifies whether ‘y’ flag is used or not
unicodeIt specifies whether ‘u’ flag is used or not
lastIndexIt specifies at which index to start the next match

JavaScript RegExp Methods: JavaScript methods are the actions that can be performed on objects.

Instance Method:

If the method is called on an instance of a RegExp then it is called an instance method.

Instance MethodsDescription
execIt is used to test if the strings match
testIt also checks if the string matches but the return type is different
toStringIt returns the string value of the Regular Expression

The Different Metacharacters and Flags of RegExp is given below:

RegExp

Description

MetacharacterSearch single characters, except line terminator or newline.
 m  Perform multiline matching.
 \rThe RegExp \r Metacharacter carriage return characters.
 [abc]Search for any character which is between the brackets.
 (x|y)Search any of the specified characters (separated by |).
 \xxxFind the character specified by an octal number xxx.
 \W Find the non word character i.e. characters which are not from a to z, A to Z, 0 to 9. 
 [^abc]Search for any character which is not between the brackets.
 gFind all the occurrences of the pattern instead of stopping after the first match i.e it performs a global match.
 [0-9] Search any digit which is between the brackets.
 \sFind the whitespace characters. 
 \bFind a match at the beginning or end of a word
 i Perform case-insensitive matching in the string.
 \nThe RegExp \n Metacharacter in JavaScript is used to find the newline character.
 [^0-9] Search for any digit which is not between the brackets.
 \BFind a match that is not present at the beginning or end of a word.
 \fFind the form feed character (form feed is a page-breaking ASCII control character).
 \wFind the word character i.e. characters from a to z, A to Z, 0 to 9. It is the same as [a-zA-Z_0-9].
 \dSearch digit characters. It is the same as [0-9].
 \t If it is found it returns the position else it returns -1.
 \DSearch non-digit characters i.e all the characters except digits. It is the same as [^0-9].
 \0Find the NULL character. If it is found it returns the position else it returns -1.
 \vFind the vertical tab character. If it is found it returns the position else it returns -1.
 *Find the match of any string that contains zero or more occurrences of m.
 {X,}Find the match of any string that contains a sequence of m, at least X times, where X is a number.
 ?!Find the match of any string which is not followed by a specific string m.
 {X}Find the match of any string that contains a sequence of m, X times where X is a number.
 ^Find the match of any string which contains m at the beginning of it.
 ?Find the match of any string that contains zero or one occurrence of m.
 $Find the match of any string which contains m at the end of it.
 +Find the match of any string that contains at least one m.
 \uxxxxFind the Unicode character specified by a hexadecimal number XXXX.
 {X,Y}Find the match of any string that contains a sequence of m, X to Y times where X, Y must be numbered.


Next Article
JavaScript RegExp ?! Quantifier
author
kartik
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JavaScript-RegExp

Similar Reads

  • JavaScript RegExp * Quantifier
    The RegExp m* Quantifier in JavaScript is used to find the match of any string that contains zero or more occurrences of match. [GFGTABS] JavaScript let str = "GeeksforGeeks@_123_G$"; let regex = /ke*/gi; let match = str.match(regex); console.log("Found " + match.length + "
    1 min read
  • JavaScript RegExp + Quantifier
    The m+ Quantifier in JavaScript is used to find the match of any string that contains at least one m. [GFGTABS] JavaScript let str = "GeeksforGeeks@_123_$"; let regex = /G+/gi; let match = str.match(regex); console.log("Found " + match.length + " matches: " + match); [/
    1 min read
  • JavaScript RegExp $ Quantifier
    The RegExp m$ Quantifier in JavaScript is used to find the match of any string which contains m at the end of it. [GFGTABS] JavaScript let str = "Geeksforh\nGeeks@_h"; let regex = /h$/gim; let match = str.match(regex); console.log("Found " + match.length + " matches: "
    1 min read
  • JavaScript RegExp ?! Quantifier
    The RegExp ?!m Quantifier in JavaScript is used to find the match of any string which is not followed by a specific string m. [GFGTABS] JavaScript // 3-digits not followed by any numbers const str = "123Geeks12345@"; const regex = /\d{3}(?!\d)/g; const match = str.match(regex); console.log
    1 min read
  • JavaScript RegExp . Metacharacter
    The . metacharacter in JavaScript regular expressions matches any single character except for a newline (\n) or other line terminators, such as \r. It is widely used as a wildcard to represent "any character." [GFGTABS] JavaScript let regex = /c.t/; let str1 = "cat"; let str2 = "cut
    3 min read
  • JavaScript RegExp g Modifier
    The g (global) modifier in JavaScript regular expressions is used to perform a global search. It ensures the pattern is matched multiple times throughout the entire string, rather than stopping at the first match. [GFGTABS] JavaScript let regex = /cat/g; let str = "cat, caterpillar, catch a cat
    3 min read
  • JavaScript RegExp Metacharacter
    The \b metacharacter in JavaScript regular expressions represents a word boundary, allowing you to match positions where a word begins or ends. A word boundary is the position between a word character (\w: letters, digits, or underscores) and a non-word character (\W: everything else, including spac
    3 min read
  • JavaScript RegExp Metacharacter
    The \r metacharacter in JavaScript regular expressions matches a carriage return character. A carriage return is a special control character (\r) with the ASCII code 13, often used to represent the end of a line in text files created on older operating systems (e.g., Windows uses \r\n for line break
    2 min read
  • JavaScript RegExp m Modifier
    The m modifier in JavaScript regular expressions stands for "multiline". It alters the behavior of the ^ (caret) and $ (dollar) anchors, allowing them to match the start or end of any line in a multiline string, rather than just the start or end of the entire string. [GFGTABS] JavaScript let regex =
    3 min read
  • JavaScript RegExp
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences