📜  JavaScript RegExp exec()方法

📅  最后修改于: 2020-10-25 10:38:45             🧑  作者: Mango

JavaScript RegExp.prototype.exec()方法

exec()方法返回包含所有匹配组的数组。它执行对指定字符串的匹配项的搜索。如果找到匹配项,则返回一个数组。否则,它返回null。

句法

RegExpObject.exec( string )

参量

字符串 :要搜索的字符串。

返回值

如果找到匹配项,它将返回数组。否则,它将返回null。

例子1

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);

输出:

Test 1 - returned value : script

例子2

var str = "Javascript is an interesting scripting language";
 var obj = new RegExp( "output", "g" );
         var res = obj.exec(str);
  document.writeln("
Test 2 - returned value : " + res);

输出:

Test 2 - returned value : null

例子3

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("
Returned value : " + res); // use,document.write

输出:

Returned value : script
Returned value : language