我有这个函数,按给定的字母数划分给定的文章,但它也在行尾拆分单词,如果单词没有完成/拆分,我想在行尾使用连字符。
var text = `Lorem Ipsum 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 software like Aldus PageMaker including versions of Lorem Ipsum.`
function divideTextByLetterCount(text, letterCount) {
let dividedText = "";
let currentLetterCount = 0;
for (let i = 0; i < text.length; i++) {
dividedText += text[i];
currentLetterCount++;
if (currentLetterCount === letterCount) {
if (dividedText.slice(-1) == ' ') {
dividedText = dividedText.slice(0, -1)
}
dividedText += "n";
currentLetterCount = 0;
}
}
let dividedTextArr = dividedText.split('n');
dividedTextArr.forEach((val, i) => {
if (val.slice(0, 1) == ' ') {
dividedTextArr[i] = val.slice(1);
}
});
return dividedTextArr.join('n');
}
console.log(divideTextByLetterCount(text, 20));
输出是
Lorem Ipsum is simpl
y dummy text of the
printing and typeset
ting industry. Lorem
Ipsum has been the
industry's standard
dummy text ever sinc
e the 1500s, when an
unknown printer too
k a galley of type a
nd scrambled it to m
ake a type specimen
book. It has survive
d not only five cent
uries, but also the
leap into electronic
typesetting, remain
ing essentially unch
anged. It was popula
rised in the 1960s w
ith the release of L
etraset sheets conta
ining Lorem Ipsum pa
ssages, and more rec
ently with desktop p
ublishing software l
ike Aldus PageMaker
including versions o
f Lorem Ipsum.
但是它应该在以未完成的单词结尾的行末尾添加连字符,例如simpl
必须是simpl-
或sinc
必须是sinc-
,我如何创建该逻辑?谢谢。
一种更快、更容易读的方法是使用正则表达式,它也可以实现您正在寻找的功能。匹配最多20个字符,并为最后一个字符后面的单个非空格字符放置一个可选的捕获组。如果捕获组捕获了任何内容,则说明您处于单词中间,并且可以添加-
.
const text = `Lorem Ipsum 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 software like Aldus PageMaker including versions of Lorem Ipsum.`
function divideTextByLetterCount(text, letterCount) {
const pattern = new RegExp(`.{0,${letterCount}}(?=(\S)?)`, 'g');
return text
.replace(
pattern,
(match, nextChar) => match.trim() + (!match.endsWith(' ') && nextChar ? '-' : '') + 'n'
);
}
console.log(divideTextByLetterCount(text, 20));
用50代替20的例子:
const text = `Lorem Ipsum 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 software like Aldus PageMaker including versions of Lorem Ipsum.`
function divideTextByLetterCount(text, letterCount) {
const pattern = new RegExp(`.{0,${letterCount}}(?=(\S)?)`, 'g');
return text
.replace(
pattern,
(match, nextChar) => match.trim() + (!match.endsWith(' ') && nextChar ? '-' : '') + 'n'
);
}
console.log(divideTextByLetterCount(text, 50));