The substr should not consider html tags in its count, is there any way to achieve it?
for example, if I have
input = <p> This is testing <strong> hi </strong> how are you, Well doing </p>
The input.substring(0, 20) should not consider <p> and <strong> tags in its character count.
The html tags should not be removed from the string, just they have to be ignore in the count for substring.
<h2 v-html="this.$options.filters.limitText(input)" ></h2>
filters: {
limitText: function (val) {
if(val && (val.length > 20)) {
return (val.substring(0, 20).replace(/\s\S*$/, '') + ' ...')
}
return val;
}
}
Does this help:
const input = "<p> This is testing <strong> hi </strong> how are you, Well doing </p>";
const tags = /<[^>]*>/g;
const output = input.replace(tags, '').substring(0, 20);
console.log(output); //-> ' This is testing hi'
My suggestion:
const input = "<p> This is testing <strong> hi </strong> how are you, Well doing </p>";
const count = input.replace(/(<([^>]+)>)/gi, "").length;
console.log(count); // Output: 47