JavaScript | Regular Expressions
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. 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;
Examples:
var patt = /GeeksforGeeks/i;
Explanation:
/GeeksforGeeks/i is a regular expression.
GeeksforGeeks is a pattern (to be used in a search).
i is a modifier (modifies the search to be Case-Insensitive).
Regular Expression Modifiers :
Modifiers can be used to perform multiline searches:
Examples:
| Expressions | Description |
|---|---|
| [abc] | Find any of the character inside the brackets |
| [0-9] | Find any of the digits between the brackets 0 to 9 |
| (x | y) | Find any of the alternatives between x or y separated with | |
Regular Expression Patterns :
Metacharacters are characters with a special meaning:
Examples:
| Metacharacter | Description |
|---|---|
| \d | Used to find a digit |
| \s | Used to find a whitespace character |
| \b | Used to find a match at beginning or at the end of a word |
| \uxxxx | Used to find the Unicode character specified by the hexadecimal number xxxxx |
Quantifiers define quantities:
Examples:
| Quantifier | Description |
|---|---|
| n+ | Used to match any string that contains at least one n |
| n* | Used to match any string that ccontains zero or more occurrences of n |
| n? | Used to matches any string that contains zero or one occurrences of n |
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 Regular Expression :
Use a regular expression to do a case-insensitive search for “GeeksforGeeks” in a string:
Example :
<script> function myFunction() { // input string var str = "Visit geeksforGeeks!"; // searching string with modifier i var n = str.search(/GeeksforGeeks/i); document.write(n + '<br>'); // searching string without modifier i var n = str.search(/GeeksforGeeks/); document.write(n); } myFunction(); </script> |
Output:
6 -1
Use String replace() With a Regular Expression :
Use a case insensitive regular expression to replace gfG with GeeksforGeeks in a string:
Example :
<script> function myFunction() { // input string var str = "Please visit gfG!"; // replacing with modifier i var txt = str.replace(/gfg/i, "geeksforgeeks"); document.write(txt); } myFunction(); </script> |
Output:
Please visit geeksforgeeks!
Recommended Posts:
- JavaScript | Immediately Invoked Function Expressions (IIFE)
- How to validate URL using regular expression in JavaScript?
- Implement prepend and append with regular JavaScript
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- JavaScript Course | Understanding Code Structure in JavaScript
- JavaScript Course | Printing Hello World in JavaScript
- JavaScript Course | Conditional Operator in JavaScript
- JavaScript Course | Logical Operators in JavaScript
- JavaScript Course | Data Types in JavaScript
- Difference between regular functions and arrow functions
- JavaScript Course | JavaScript Prompt Example
- JavaScript Course | Objects in JavaScript
- JavaScript Course | Loops in JavaScript
- JavaScript Course | Operators in JavaScript
- JavaScript Course | Variables in JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



