The code removes xml blocks of certain tag names, e.g. .... and auto close tags .e.g.
usage:
removeXmlElements (hay, [...elements])
e.g.
const result =removeXmlElements ("hello world", ["b"])
the result will be hello
const hay = `<a>yes yes<w:sdt id="a">hello world</w:sdt>nono</a>
<w:tcWa id="2"/>aaaa`
const removeXmlElements = (hay, elements) =>{
const isTagAutoClose = (hay, tag) =>{
let ret = false
//situation one with no attributes
const regexp1 = new RegExp(`<${tag}/>`);
ret = regexp1.test(hay)
if (!ret) {
const regexp2 = new RegExp(`<${tag} [^<>]*?/>`);
ret = regexp2.test(hay)
}
return ret
}
let ret = hay
for (const item of elements) {
//situation one ele with no attibutes
const isAutoClose = isTagAutoClose(hay, item)
if (isAutoClose) {
const regexp1 = new RegExp(`<${item}/>`, "g");
ret = ret.replace(regexp1, "")
//situation two ele with attributes
const regexp2 = new RegExp(`<${item} [^<>].*?/>`, "gs");
ret = ret.replace(regexp2, "")
}else {
const regexp1 = new RegExp(`<${item}>.*?</${item}>`, "gs");
ret = ret.replace(regexp1, "")
//situation two ele with attributes
const regexp2 = new RegExp(`<${item} [^<>].*?>.*?</${item}>`, "gs");
ret = ret.replace(regexp2, "")
}
}
return ret
}
console.log(removeXmlElements(hay, ["w:tcWa", "w:sdt"]));
cricketis a tag. Are you sure you want to remove that?