使用JavaScript如何在特定长度打破字符串,保留特殊字符?

时间:2016-02-17 00:30:26

标签: javascript regex

我有一段文字:

George wanted to bake cookies. He asked Mom to help him.

George decided he would bake chocolate-chip cookies. First, Mom had to buy the ingredients. She bought flour, sugar, and chocolate chips. Next, George and Mom mixed together all the ingredients. Finally, they put the cookies in the oven for ten minutes. Last, they let the cookies cool down, and ate them.

"These are delicious!" said George.

我想将其转换为一个数组,每行最多包含50个字符。

我目前:

var lines = text.split(/\r|\n/g);
lines.forEach(function (item, index) {
  words = item.match(/.{1,50}\b/g);
});

这几乎可行,但它忽略了那些时期和类似的东西,所以我离开了

George wanted to bake cookies. He asked Mom to
help him

George decided he would bake chocolate-chip
cookies. First, Mom had to buy the ingredients.
She bought flour, sugar, and chocolate chips. Next
, George and Mom mixed together all the
ingredients. Finally, they put the cookies in the
oven for ten minutes. Last, they let the cookies
cool down, and ate them

"These are delicious!" said George.

我做错了什么?

2 个答案:

答案 0 :(得分:1)

您可以使用空格而不是单词边界(在字母和点之间匹配):

例如使用此模式:

/\S(?:.{0,48}\S)?(?!\S)/g

细节:

\S              # all that is not a white-space
(?:.{0,48}\S)?  # between 0 and 48 characters followed by a non white character
(?!\S)          # not followed by a non-white character (in other words, followed
                # by a white space or the end of the string)

另一个优点是模式避免了前导和尾随空格。

答案 1 :(得分:0)

您正在使用g全局修饰符。返回所有匹配,而不仅仅是第一个匹配。你似乎只需要前50个字符。锚b将保留最后一个单词不被破坏,因此实际字符串可能少于50个字符。这应该会给你正确的结果:

var lines = text.split(/\r|\n/g);
lines.forEach(function (item, index) {
  words = item.match(/.{1,50}\b/);
  alert(words);
});

如果您想要保留空格和标点符号,您必须进一步修改。