Javascript 为什么我的正则表达式不在JS中工作?
我有一个regex,它在regex101中运行良好,但当我尝试将它放入js函数时,它不起作用,这意味着它给出了错误的结果 我需要Javascript 为什么我的正则表达式不在JS中工作?,javascript,regex,Javascript,Regex,我有一个regex,它在regex101中运行良好,但当我尝试将它放入js函数时,它不起作用,这意味着它给出了错误的结果 我需要数据sh content和数据sh attr的值。由于某些原因,我不能使用dom操作。它必须使用正则表达式 var content = '<p><!-- content block start --></p><div class="content-block mceNonEditable" style="color: #9999
数据sh content
和数据sh attr
的值。由于某些原因,我不能使用dom操作。它必须使用正则表达式
var content = '<p><!-- content block start --></p><div class="content-block mceNonEditable" style="color: #999999;" data-sh-attr="%20color%3D%22%23999999%22" data-sh-content="%5Bgrid%5D%3Cp%3Elkjlk%3C%2Fp%3E%5B%2Fgrid%5D%5Bgrid%5D%3Cp%3Elkjlkj%3C%2Fp%3E%5B%2Fgrid%5D"><div class="toolbar"><button class="edit-content-block">Edit</button><button class="remove-content-block">Delete</button></div><div class="content"><div class="grid col-md-6" data-sh-attr="" data-sh-content="%3Cp%3Elkjlk%3C%2Fp%3E"><p>lkjlk</p></div><div class="grid col-md-6" data-sh-attr="" data-sh-content="%3Cp%3Elkjlkj%3C%2Fp%3E"><p>lkjlkj</p></div><div';
console.log('before restoring shortcode: %s', content);
var content_block_pattern = /<p><!-- content block start -->[\S\s]+?content-block[\S\s]+?data-sh-attr="([\s\S]+?)"[\s\S]+?data-sh-content="([\s\S]+?)"[\S\s]+?<!-- content block end --><\/p>/g;
content = content.replace(content_block_pattern, function(attributes, content){
attributes = decodeURIComponent(attributes);
content = decodeURIComponent(content);
console.log("attributes %s", attributes);
console.log("Conents: %s", content);
return '[block '+attributes+']'+content+'[/block]';
});
console.log('after restoring shortcodes: %s', content);
var content='EditDeletelkjlklkjlkj不要使用正则表达式处理HTML。尤其是当您实际使用世界上最复杂的HTML解析器时
// parse HTML string into a DOM node
var content = '<p><!-- content block start --></p><div class="content-block mceNonEditable" style="color: #999999;" data-sh-attr="%20color%3D%22%23999999%22" data-sh-content="%5Bgrid%5D%3Cp%3Elkjlk%3C%2Fp%3E%5B%2Fgrid%5D%5Bgrid%5D%3Cp%3Elkjlkj%3C%2Fp%3E%5B%2Fgrid%5D"><div class="toolbar"><button class="edit-content-block">Edit</button><button class="remove-content-block">Delete</button></div><div class="content"><div class="grid col-md-6" data-sh-attr="" data-sh-content="%3Cp%3Elkjlk%3C%2Fp%3E"><p>lkjlk</p></div><div class="grid col-md-6" data-sh-attr="" data-sh-content="%3Cp%3Elkjlkj%3C%2Fp%3E"><p>lkjlkj</p></div>';
var container = document.createElement("DIV");
container.innerHTML = content;
// helper that can process an attribute of a node
function processAttribute(elem, attrib, func) {
var val = elem.getAttribute(attrib);
if (val) elem.setAttribute(attrib, func(val));
}
// now processing the attributes is straight-forward
var contentSel = ".content-block, .content-block *[data-sh-attr], content-block *[data-sh-content]",
contentElems = container.querySelectorAll(contentSel),
i;
for (i = 0; i < contentElems.length; i++) {
processAttribute(contentElems[i], "data-sh-attr", decodeURIComponent);
processAttribute(contentElems[i], "data-sh-content", decodeURIComponent);
}
console.log(container.innerHTML);
//将HTML字符串解析为DOM节点
var content='EditDeletelkjlklkjlkj;
var container=document.createElement(“DIV”);
container.innerHTML=内容;
//可以处理节点属性的帮助器
函数processAttribute(元素、属性、函数){
var val=elem.getAttribute(attrib);
if(val)elem.setAttribute(attrib,func(val));
}
//现在,处理属性是简单的
var contentSel=“.content block、.content block*[data sh attr],content block*[data sh content]”,
contentElems=container.querySelectorAll(contentSel),
我
对于(i=0;i
我假设实际上您已经有了DOM节点,在这种情况下,不需要解析步骤。替换回调函数第一个参数是全部,而不是第一个匹配的字符串。下面是固定代码
content = content.replace(content_block_pattern, function(all, attributes, content){
attributes = decodeURIComponent(attributes);
content = decodeURIComponent(content);
console.log("attributes %s", attributes);
console.log("Conents: %s", content);
return '[block '+attributes+']'+content+'[/block]';
});
为什么在HTML上使用正则表达式?请阅读我的问题。我别无选择。你的问题中没有任何东西表明你别无选择。您坐在内置于高级HTML DOM解析器中的JavaScript前面,当然您有选择。[\S\S]
?为什么不
?@Andrea,因为
与换行符不匹配。这个答案是断章取义的。我的问题是为什么给定的正则表达式没有给出预期的结果,而正则表达式在我提供的regex101链接上似乎是正确的。@Sisir“出于某种原因”并不是一个足够有力的解释。您使用JavaScript,我向您展示了如何使用JavaScript,请解释为什么您不能使用它。