El substr no debe considerar las etiquetas html en su cuenta, ¿hay alguna forma de lograrlo?
por ejemplo, si tengo
input = <p> This is testing <strong> hi </strong> how are you, Well doing </p> El input.substring(0, 20) no debe considerar las etiquetas <p> y <strong> en su recuento de caracteres.
Las etiquetas html no deben eliminarse de la cadena, solo deben ignorarse en el recuento de la subcadena.
<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; } }¿Esto ayuda?
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'Mi sugerencia:
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