exec() - JavaScript Tutorial

exec()

BLUF: This tutorial on exec() provides an in-depth look at JavaScript's core features. It includes practical examples and code snippets to help you master modern JS development.
Key Discovery: exec()

Understanding exec() is crucial for building dynamic, interactive web applications. Explore the examples below to see it in action.

The exec function provides an array that includes all identified groups. This function conducts a search within a given string to find a match. When a match is found, an array is returned; otherwise, null is returned.

Syntax

Example

RegExpObject.exec( string )

Parameters

string : The string to be searched.

Return value

If a match is discovered, the function will return the array; otherwise, it will return null.

Example 1

Example

var str = "Javascript is an interesting scripting language";

         var re = new RegExp( "script", "g" );

        var result = re.exec(str);

        document.writeln("Test 1 - returned value : " +  result);

Output:

Output

Test 1 - returned value : script

Example 2

Example

var str = "Javascript is an interesting scripting language";

 var obj = new RegExp( "output", "g" );

         var res = obj.exec(str);

  document.writeln("<br />Test 2 - returned value : " +  res);

Output:

Output

Test 2 - returned value : null

Example 3

Example

var string  = "Javascript is an interesting scripting language";

         var obj1 = new RegExp( "script", "g" );

         

         var result1 = obj1.exec(string);

        document.write("Returned value : " +  result1);



        var obj = new RegExp( "language", "g" );

         

         var res = obj.exec(string);

       document.write("<br />Returned value : " +  res); 

// use,document.write

Output:

Output

Returned value : script

Returned value : language

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience