Friday, September 5, 2025
HomeLanguagesJavascriptJavaScript RegExp exec() Method

JavaScript RegExp exec() Method

The exec() Method in JavaScript is used to test for the match in a string. If there is a match this method returns the first match else it returns NULL.

Syntax:

RegExpObject.exec(str)

Parameters:

  • str: It is the string to be searched. This is a required field. 

Return value:

  • This method returns an array that contains the matched text if the match is found, else it returns null.

Example 1: This example searches for the string “computer” in the original string. 

Javascript




function geek() {
    let str =
        "neveropen is the " +
        "computer science portal for neveropen.";
 
    let regex = new RegExp("computer",);
    // match "computer" in string.                      
    let rex = regex.exec(str);
    console.log(rex.length);
    console.log(rex)
}
geek()


Output

1
[
  'computer',
  index: 21,
  input: 'neveropen is the computer science portal for neveropen.',
  groups: undefined
]

Example 2: This example searches for the string “rep” in the original string.  

Javascript




function geek() {
    let str =
        "neveropen is the" +
        " computer science " +
        "portal for neveropen.";
    let regex = new RegExp("rep");
 
    // Match "rep" in string.
    let rex = regex.exec(str);
    console.log(rex);
}
geek()


Output

null

We have a complete list of Javascript Functions, to check those go through the Javascript Functions Complete Reference article.

Supported Browsers: The browsers supported by the JavaScript exec() Method are listed below: 

  • Google Chrome
  • Apple Safari
  • Mozilla Firefox
  • Opera
  • Internet Explorer

We have a complete list of Javascript RegExp expressions, to check those please go through this JavaScript RegExp Reference article.

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6638 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11866 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7027 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS