Reputation: 7384
content = '5<x<div></div>'
Basically I am looking for a regular expression that will make the string like above into 5<x<div></div>
5x<div></div>
will still be 5x<div></div>
. I am just trying to escape unclosed html tags
If there is such a library then I will be very happy to use it as long as it meets my main goal of trying to escape unclosed html tags
Upvotes: 4
Views: 1518
Reputation: 3589
This is not the most beautiful solution to this task but it works.
var str = '5<x<div>s>7</div>';
for (var i = 0; i < 2; i++) {
if (i === 0) {
var str2 = str.replace(/</gi, ",,#*&,,<");
var spl = str2.split(",,#*&,,");
} else {
var str2 = str.replace(/>/gi, ">,,#*&,,");
var spl = str2.split(",,#*&,,");
}
replaceString(spl);
}
function replaceString(spl) {
for (let i = 0; i < spl.length; i++) {
if (spl[i].indexOf('<') > -1 && spl[i].indexOf('>') > -1) {
//.......
} else {
if (spl[i].indexOf('<') > -1) {
spl[i] = spl[i].replace(/</gi, "<");
}
else if (spl[i].indexOf('>') > -1) {
spl[i] = spl[i].replace(/>/gi, ">");
}
}
}
str = spl.join('');
}
console.log(str);
Upvotes: 1