我有一个与我的数组匹配的正则表达式,但它没有显示最后一个括号。我的JS代码
function decodeEntities(s){
var str, temp= document.createElement('p');
temp.innerHTML= s;
str= temp.textContent || temp.innerText;
temp=null;
return str;
}
var code = ["<span>","<div>","<pre>","<spat>"];
var string = code.join(", ");
var string_to_pass="<span";
var re = new RegExp("(?:^|\\W)" + decodeEntities(string_to_pass) + "(\\w+)(?!\\w)", "g"), match, matches = [];
while (match = re.exec(string)) {
matches.push(match[0]);
}
console.log(matches);
我在控制台中看到一个没有最后一个关闭括号的两个匹配的数组。
Array [ "<span", " <spat" ]
如何阻止此脚本删除最后一个括号>
。请提前帮助
答案 0 :(得分:1)
将正则表达式更改为,
var re = new RegExp("(?:^|\\W)" + decodeEntities(string_to_pass) + "(\\w+\\W)", "g"), match, matches = [];