📅  最后修改于: 2020-10-25 10:38:45             🧑  作者: Mango
exec()方法返回包含所有匹配组的数组。它执行对指定字符串的匹配项的搜索。如果找到匹配项,则返回一个数组。否则,它返回null。
RegExpObject.exec( string )
字符串 :要搜索的字符串。
如果找到匹配项,它将返回数组。否则,它将返回null。
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
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
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