javascript逐字断句 [英] javascript break sentence by words

查看:92
本文介绍了javascript逐字断句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将完整单词及其后继字符放入数组的好策略是什么.

Whats a good strategy to get full words into an array with its succeeding character.

示例. 这是一个了不起的句子.

Example. This an an amazing sentence.

Array(
[0] => This 
[1] => is
[2] => an
[3] => amazing
[4] => sentence.
)

元素0-3将有一个后续空格,因为句点在第4个元素之后.

Elements 0 - 3 would have a succeeding space, as a period succeeds the 4th element.

我的口头禅是.. 嘿,我需要您用空格分隔这些字符, 然后,一旦注入了数组元素的元素宽度达到X, 换行.

My idea in words is to say.. Hey, i need you to split these by spacing character, Then once width of element with injected array elements reaches X, Break into a new line.

请,请不要给出大量的代码.我宁愿写我自己的书,只是告诉我你会怎么做.

Please, gawd don't give tons of code. I prefer to write my own just tell me how you would do it.

推荐答案

只需使用

,如果您需要一个空格,为什么不这样做呢? (之后使用循环)

and if you need it with a space, why don't you just do that? (use a loop afterwards)

var str = "This is an amazing sentence.";
var words = str.split(" ");
for (var i = 0; i < words.length - 1; i++) {
    words[i] += " ";
}
console.log(words);
//["This ", "is ", "an ", "amazing ", "sentence."]

哦,睡个好觉!

这篇关于javascript逐字断句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆