im here to ask, if someone can help me with quotation and syntax checking. im aware of the question solution 1, but i don't fully understand that solution.
here is an example, fully integrated into a website and humbly ask, if anyone can find an error?
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>
You can simplify your regex and capture groups of "..."
and then replace the quotes of those groups with your desired ones:
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);