jQuery javascript逐句断句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18473326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:42:12  来源:igfitidea点击:

javascript break sentence by words

javascriptjquery

提问by THE AMAZING

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.
)

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

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

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.

我的想法是说..嘿,我需要你按间距字符拆分这些,然后一旦注入数组元素的元素宽度达到X,就换行。

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

请不要给大量的代码。我更喜欢自己写,只是告诉我你会怎么做。

回答by h2ooooooo

Just use split:

只需使用split

var str = "This is an amazing sentence.";
var words = str.split(" ");
console.log(words);
//["This", "is", "an", "amazing", "sentence."]

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."]

Oh, and sleep well!

哦,睡个好觉!

回答by Isaac

Similar to Ravi's answer, use match, but use the word boundary \bin the regex to split on word boundaries:

Ravi 的回答类似,使用match,但使用\b正则表达式中的词边界来分割词边界:

'This is  a test.  This is only a test.'.match(/\b(\w+)\b/g)

yields

产量

["This", "is", "a", "test", "This", "is", "only", "a", "test"]

or

或者

'This is  a test.  This is only a test.'.match(/\b(\w+\W+)/g)

yields

产量

["This ", "is  ", "a ", "test.  ", "This ", "is ", "only ", "a ", "test."]

回答by cars10m

try this

尝试这个

var words = str.replace(/([ .,;]+)/g,'§sep§').split('§sep§');

This will

这会

  1. insert a marker §sep§after every chosen delimiter [ .,;]+
  2. split the string at the marked positions, thereby preserving the actual delimiters.
  1. §sep§在每个选定的分隔符后插入一个标记[ .,;]+
  2. 在标记的位置拆分字符串,从而保留实际的分隔符。

回答by Ravi Rajendra

If you need spaces and the dots the easiest would be.

如果你需要空格和点,最简单的就是。

"This is an amazing sentence.".match(/.*?[\.\s]+?/g);

the result would be

结果是

['This ','is ','an ','amazing ','sentence.']

回答by doogle

Here is an option if you wanted to include the space and complete in O(N)

如果您想包含空格并在 O(N) 中完成,这是一个选项

var str = "This is an amazing sentence.";
var words = [];
var buf = "";
for(var i = 0; i < str.length; i++) {
    buf += str[i];
    if(str[i] == " ") {
        words.push(buf);
        buf = "";
    }
}

if(buf.length > 0) {
    words.push(buf);
}

回答by Penny Liu

This can be done with lodash _.words:

这可以通过 lodash 完成_.words

var str = 'This is an amazing sentence.';
console.log(_.words(str, /[^, ]+/g));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>