我需要在50个字符之后缩短文本,但是如果第50个字符位于单词的中间-只在单词之后而不是在单词之前剪切。
示例文本:与普遍的看法相反,Lorem Ipsum不是简单的文本(59个字符)
与普遍的看法相反,Lorem Ipsum不是简单的(54个字符)我在尝试这个:text.substr(0, 50)。lastIndexOf (' '));-对我来说不太好,因为它减少了"简单"。我想要的是简短的文本。谢谢你的帮助!
如果短语长度小于限制,则返回该短语;否则,将切片索引设置为limit并扫描,直到找到单词边界并对短语进行切片。
const clip = (phrase, limit) => {
if (phrase.length <= limit) return phrase;
let sliceIndex = limit;
while (sliceIndex < phrase.length && /b/.test(phrase[sliceIndex])) sliceIndex++;
return phrase.slice(0, sliceIndex);
};
const input = "Contrary to popular belief, Lorem Ipsum is not simply text";
const expected = "Contrary to popular belief, Lorem Ipsum is not simply";
const actual = clip(input, 50);
console.log(actual === expected); // true
正则表达式可以帮助您。
我们的想法是找到至少50个以单词边界b
结尾的符号
let text = 'Contrary to popular belief, Lorem Ipsum is not simply text';
let shortetedText = text.match(/.{50,}?(?=b)/)[0];
console.log(shortetedText);
UPD:
如果你想创建一个函数来缩短你的文本,如果它超过50个符号,我们可以创建一个函数来代替你。
let short = text => text.length > 50 ? text.match(/.{50,}?(?=b)/)[0]: text;
然后像这样使用:
let longText = 'Contrary to popular belief, Lorem Ipsum is not simply text';
let shortText = 'Lorem ipsum';
console.log(short(longText)); //will shorten text
console.log(short(shortText)); //will leave text as it is bcoz it's shorter than 50 symbols
您接近子字符串&indexOf。如果你在前50个字符之后找一个空格,就可以了。还有一种极端情况,即字符串长度大于50个字符且没有空格。
const shortener = (str) => {
const offset = str.substring(50).indexOf(' ');
return offset !== -1 ? str.substring(0,50 + offset) + '...' :
str.length > 50 ? str + '...' : str;
}
const text ="Contrary to popular belief, Lorem Ipsum is not simply text"
// start of the last word
const start = text.substr(0,50).lastIndexOf(' ')
// start from where you left + 1 to remove space
const end = text.substr(start+1,text.length).indexOf(' ')
console.log(text.substr(0,start + end + 1));
正则表达式一行程序
下面的部分正则表达式可能看起来是多余的,但它们实际上是为了覆盖边缘情况。
// Length fixed at "50"
const trim50 = str => str?.match(/^.{0,50}.*?(?=b| |$)/)[0];
// Dynamic length
const trim = (str, n) => new RegExp(`^.{0,${n}}.*?(?=b| |$)`).exec(str)[0];
// Demo
console.log(trim('Contrary to popular belief, Lorem Ipsum is not simply text', 50))
Regex-free
const trim = (str, n) => {
const start = str.substring(0, n);
const index = str.indexOf(' ', n - 1);
const end = str.slice(n, index < 0 ? str.length : index);
return start + end;
}
console.log(trim('Contrary to popular belief, Lorem Ipsum is not simply text', 50));
测试用例由于SO的内置控制台的限制,请首先用F12打开DevTools的控制台,然后运行下面的代码来做一个小演示。
const testCases = [
{ input: 'Contrary to popular belief, Lorem Ipsum is not simply text',
expected: 'Contrary to popular belief, Lorem Ipsum is not simply' },
{ input: '1234 6789 1234 6789 1234 6789 1234 6789 1234 678901234 6789',
expected: '1234 6789 1234 6789 1234 6789 1234 6789 1234 678901234' },
{ input: '1234 6789 1234 6789 1234 6789 1234 6789 1234 6789 1234 6789',
expected: '1234 6789 1234 6789 1234 6789 1234 6789 1234 6789 ' },
{ input: '1234 6789 1234 6789 1234 6789 1234 6789 1234 67890123456789',
expected: '1234 6789 1234 6789 1234 6789 1234 6789 1234 67890123456789' },
{ input: ' ',
expected: ' ' },
{ input: '1234 5678',
expected: '1234 5678' },
{ input: '1234',
expected: '1234' },
{ input: '',
expected: '' }
];
const test = trim => {
console.table(
testCases.map(
testCase => Object.assign(
Object.assign(testCase, { output: trim(testCase.input) }),
{ pass: testCase.output === testCase.expected }
)
)
);
};
test(str => str?.match(/^.{0,50}.*?(?=b| |$)/)[0]);