He usado el filtro vue para limitar el texto a 100 caracteres. Estoy obteniendo salida como
Tat is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the indu ... Si ves la última palabra indu ... ,. No quiero tener una división de palabras en el medio con algunos caracteres de la palabra y puntos, sino que quiero que sea como la palabra completa y luego los puntos, como a continuación:
Tat is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's ... La palabra debe completarse después de 100 caracteres y luego ... debe agregarse.
A continuación se muestra el filtro Vue que he usado, ¿cómo puedo hacer que termine con la palabra completa y luego puntos en lugar de algunos caracteres de la última palabra?
msg = "<p> Tat is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing</p>\n" <h2 v-html="this.$options.filters.limitText(msg)" ></h2> filters: { limitText: function (val) { if(val && (val.length > 100) { return (val.substr(0, 100) + ' ...') } return val; } }1- obtener los primeros 100 caracteres
2- corte hasta el índice del siguiente espacio o final del texto.
const msg = `Tat is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing` const LAST_INDEX = 99 let msgShort = msg.substring(0,LAST_INDEX)+msg.slice(LAST_INDEX,msg.indexOf(' ',LAST_INDEX))+'...' console.log(msgShort)substr . use substring en su lugar.Usando una expresión regular, se me ocurrió esta solución:
const msg = `Tat is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing` console.log(msg.substr(0, 100).replace(/\s\S*$/, ''))Básicamente, después de extraer los 100 primeros caracteres, uso una expresión regular para encontrar el último espacio seguido de caracteres que no son espacios y los elimino.