Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript正则表达式括号子匹配数组_Javascript - Fatal编程技术网

Javascript正则表达式括号子匹配数组

Javascript正则表达式括号子匹配数组,javascript,Javascript,是否可以将子匹配作为数组而不是参数获取 像这样: text.replace(/^(?!(^$)|\<(h[0-9])\>)(.*)$/mg, function(match, submatches) { return '<p>'+submatches[2]+'</p>'; }); text.replace(/^(?(^$)|\)(.*)$/mg,函数(匹配,子匹配){ 返回“”+子匹配[2]+'”; }); 与此相反: text.replace(/^

是否可以将子匹配作为数组而不是参数获取

像这样:

text.replace(/^(?!(^$)|\<(h[0-9])\>)(.*)$/mg, function(match, submatches) {
    return '<p>'+submatches[2]+'</p>';
});
text.replace(/^(?(^$)|\)(.*)$/mg,函数(匹配,子匹配){
返回“”+子匹配[2]+'

”; });
与此相反:

text.replace(/^(?!(^$)|\<(h[0-9])\>)(.*)$/mg, function(match, p1, p2, p3) {
    return '<p>'+p3+'</p>';
});
text.replace(/^(?(^$)|\)(.*)$/mg,函数(匹配,p1,p2,p3){
返回“”+p3+”

”; });
您可以使用rest运算符将所有参数聚合为单个参数数组:

text.replace(/^(?!(^$)|\<(h[0-9])\>)(.*)$/mg, function(match, ...submatches) {
    return '<p>' + submatches[2] + '</p>';
});
text.replace(/^(?(^$)|\)(.*)$/mg,函数(匹配,…子匹配){
返回“”+子匹配[2]+'

”; });
您可以使用rest运算符将所有参数聚合为单个参数数组:

text.replace(/^(?!(^$)|\<(h[0-9])\>)(.*)$/mg, function(match, ...submatches) {
    return '<p>' + submatches[2] + '</p>';
});
text.replace(/^(?(^$)|\)(.*)$/mg,函数(匹配,…子匹配){
返回“”+子匹配[2]+'

”; });
Javascript有一个特殊的对象,它被传递到每个调用的函数调用中


这适用于旧版本的Javascript,而操作符仅在ES6中可用。

Javascript有一个特殊的对象,该对象被传递到每个调用的函数调用中


这适用于较旧版本的Javascript,而运算符仅在ES6中可用。

请注意,在您的示例中,
子匹配将不仅仅包含子匹配
.replace
会将奇怪的东西传递给函数。请注意,
子匹配
在示例中包含的不仅仅是子匹配
.replace
将奇怪的东西传递给函数。