我来这里是想问,是否有人可以帮助我进行引用和语法检查。这是一个例子,完全集成到网站中,虚心请教,是否有人能找到错误?function rewriteQuotes(){ var all_p = document.querySelectorAll(`.content_container`); var regex_s = /[\s\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,\-.\/:;<=>?@\[\]^_`{|}~]/; for (var i = 0; i < all_p.length; i++) { if (all_p[i] != null) { var all_nodes = all_p[i].querySelectorAll("*"); for (var k = 0; k < all_nodes.length; k++) { if (all_nodes[k].childNodes.length > 0) { var all_children = all_nodes[k].childNodes; for (var t = 0; t < all_children.length; t++) { if (all_children[t].nodeName == "#text") { all_children[t].textContent = all_children[t].textContent.replace(/"/g,"\“"); all_children[t].textContent = all_children[t].textContent.replace(/\s\“/g,"\ „"); if (all_children[t].textContent.charAt(0)== "\“") { if (all_children[t].textContent.charAt(1) === "") { } else { var char_1 = all_children[t].textContent.charAt(1); if (regex_s.test(char_1)) { } else { all_children[t].textContent = "\„" + all_children[t].textContent.substring(1); } } } } } } } } }};document.addEventListener('DOMContentLoaded',function(){ setTimeout(function(){ rewriteQuotes(); },1);});<div class="content_container"> <h2>"Hello"</h2> <p>my "aunti" was driving over the "cat" with her "<em>skateboard</em>".</p></div>
1 回答
慕婉清6462132
TA贡献1804条经验 获得超2个赞
您可以简化正则表达式并捕获组"...",然后将这些组的引号替换为您想要的引号:
const regex = /"(.*?)"/g;
const str = `my "aunti" was driving over the "cat" with her "car"`;
const subst = `„$1“`;
const result = str.replace(regex, subst);
console.log('Original: ', str);
console.log('Result: ', result);
添加回答
举报
0/150
提交
取消
