我使用 vue 過濾器將文本限制為 100 個字符。我得到的輸出為
Tat is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the indu ...
如果你看到最后一個字indu ...,。我不想在單詞和點的幾個字符之間進行分詞,而是希望它像完整的單詞然后是點,如下所示:
Tat is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's ...
該單詞應在 100 個字符后完成,然后...需要附加。
下面是我使用過的 Vue 過濾器,我怎樣才能以完整的單詞結尾,然后用點代替最后一個單詞的幾個字符?
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;
}
}
uj5u.com熱心網友回復:
使用正則運算式,我想出了這個解決方案:
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*$/, ''))
基本上,在提取 100 個第一個字符后,我使用正則運算式來查找后跟非空格字符的最后一個空格,并洗掉它們。
uj5u.com熱心網友回復:
1- 獲取前 100 個字符
2- 切片直到下一個空格或文本結尾的索引。
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不推薦使用。使用substring來代替。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/373166.html
上一篇:使用vue組件但顯示為空白
下一篇:如何正確安裝firebase?
