Friday, November 15, 2024
Google search engine
HomeLanguagesJavascriptJavaScript RegExp(Regular Expression)

JavaScript RegExp(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. Regular expressions can be used to perform all types of text search and text replacement operations.

Syntax:

/pattern/modifiers;

Example:

let patt = /neveropen/i;

Explanation : 

/neveropen/i is a regular expression.
neveropen is the pattern (to be used in a search).
i is a modifier (modifies the search to be Case-Insensitive).

Regular Expression Modifiers can be used to perform multiline searches which can also be set to case-insensitive matching: 

Expressions Descriptions
g Find the character globally
i Find a character with case-insensitive matching
m Find multiline matching

Regular Expression Brackets can be Find characters in a specified range

Expressions Description
[abc] Find any of the characters inside the brackets
[^abc] Find any character, not inside the brackets
[0-9] Find any of the digits between the brackets 0 to 9
[^0-9] Find any digit not in between the brackets
(x | y) Find any of the alternatives between x or y separated with |

Regular Expression Metacharacters are characters with a special meaning:

Metacharacter Description
\. Search single characters, except line terminator or newline.
\w Find the word character i.e. characters from a to z, A to Z, 0 to 9
\d Find a digit
\D Search non-digit characters i.e all the characters except digits
\s Find a whitespace character
\S Find the non-whitespace characters.
\b Find a match at the beginning or at the end of a word
\B Find a match that is not present at the beginning or end of a word.
\0 Find the NULL character.
\n Find the newline character.
\f Find the form feed character
\r Find the carriage return character
\t Find the tab character
\v Find the vertical tab character
\uxxxx Find the Unicode character specified by the hexadecimal number xxxxx

Regular Expression Quantifiers are used to define quantitiesoccurrence

Quantifier Description
n+ Match any string that contains at least one n
n* Match any string that contains zero or more occurrences of n
n? Match any string that contains zero or one occurrence of n
m{X} Find the match of any string that contains a sequence of m, X times
m{X, Y} Find the match of any string that contains a sequence of m, X to Y times
m{X,} Find the match of any string that contains a sequence of m, at least X times
m$ Find the match of any string which contains m at the end of it
^m Find the match of any string which contains m at the beginning of it
?!m Find the match of any string which is not followed by a specific string m.

Regular Expression Object Properties:

Property Description
constructor Return the function that created the RegExp object’s prototype
global Specify whether the “g” modifier is set or not
ignorecase Specify whether the “i” modifier is set or not
lastindex Specify the index at which to start the next match
multiline Specify whether the “m” modifier is set or not
source Return the text of RegExp pattern

Regular Expression Object Methods:

Method Description
compile() Used to compile the regular expression while executing of script
exec() Used to test for the match in a string.
test() Used to test for a match in a string
toString() Return the string value of the regular expression

Below is an example of the JavaScript Regular Expressions. 

Example:  

JAVASCRIPT




function GFGFun() {
    let str = "Visit neveropenforGeeks";
    let n = str.search(/neveropen/i);
    console.log(n);
}
GFGFun();


Output:  

6

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 “neveropen” in a string:

Example:

JAVASCRIPT




function myFunction() {
 
    // input string
    let str = "Visit neveropenforGeeks!";
 
    // searching string with modifier i
    let n = str.search(/neveropen/i);
 
    console.log(n);
 
    // searching string without modifier i
    let n = str.search(/neveropen/);
 
    console.log(n);
}
myFunction();


Output: 

6
-1

Use String replace() With a Regular Expression : Use a case insensitive regular expression to replace gfG with neveropen in a string:

Example: 

JAVASCRIPT




function myFunction() {
 
    // input string
    let str = "Please visit gfG!";
 
    // replacing with modifier i
    let txt = str.replace(/gfg/i, "neveropen");
     
    console.log(txt);
}
myFunction();


Output:

Please visit neveropen! 

RELATED ARTICLES

Most Popular

Recent Comments