我有一个正则表达式,它将单词与我的数组匹配,但它没有显示最后一个括号。我的 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" ]
我怎样才能阻止这个脚本删除最后一个括号>
。请帮忙提前致谢
最佳答案
将您的正则表达式更改为,
var re = new RegExp("(?:^|\\W)" + decodeEntities(string_to_pass) + "(\\w+\\W)", "g"), match, matches = [];
关于javascript - 这个表达式中最后一个括号去哪里了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28472264/