【问题标题】:JavaScript break sentence by wordsJavaScript 逐字逐句
【发布时间】:2013-08-27 18:54:39
【问题描述】:

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

示例。

这是一个惊人的句子。

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

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

我需要你用空格字符分开这些, 然后一旦注入数组元素的元素宽度达到X, 换行。

请不要提供大量代码。我更喜欢自己写,告诉我你会怎么做。

【问题讨论】:

  • 使用javascriptsplit函数。
  • 我会采用此答案stackoverflow.com/questions/4514144/… 中提供的方法。但是对于您的情况,将var newstringreplaced = string.replace(/d/gi, ",d"); 更改为var newstringreplaced = string.replace(/\s/gi, " ,");编辑: 应该注意这种方法仅在您的原始字符串没有, 时才有用。我想这个解决方案更安全:stackoverflow.com/a/4514241/1417588

标签: javascript jquery


【解决方案1】:

类似于Ravi's answer,使用match,但在正则表达式中使用单词边界\b来分割单词边界:

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

产量

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

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

产量

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

【讨论】:

  • 这确实是最好的答案,因为按空间分割并不是真正适用于现实生活场景。好吧,除非你不使用标点符号并且总是使用单个空格。
  • 将“不会”转换为“赢”和“t”。这允许收缩: str.match(/\b(\w+)'?(\w+)?\b/g)
  • 只有英文单词 :(
【解决方案2】:

只需使用split:

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

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

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

哦,睡个好觉!

【讨论】:

  • @cars10 改变了答案 - 这能解决问题吗?
  • @cars10 为什么你需要在每个单词的末尾添加空格?如果您希望在连接字符串时返回它们,只需 .join(' ');
  • 我只是在输入这个。很好,反应很快。
  • 好答案 :) 我需要空格,因为用户句子有空格,我计划重新输出他们的句子,而不必在循环中声明空格。 :) 谢谢你
  • @user2716649 正如 Dennis Martinez 所说,您可以简单地使用 words.join(" ") 再次获取 This is an amazing sentence.
【解决方案3】:

试试这个

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

这会

  1. 在每个选定的分隔符[ .,;]+ 之后插入一个标记§sep§
  2. 在标记的位置拆分字符串,从而保留实际的分隔符。

【讨论】:

    【解决方案4】:

    如果您需要空格和点,最简单的方法是。

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

    结果是

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

    【讨论】:

      【解决方案5】:

      使用splitfilter 删除前导和尾随空格。

      let str = '     This is an amazing sentence.  ',
        words = str.split(' ').filter(w => w !== '');
      
      console.log(words);

      【讨论】:

        【解决方案6】:

        如果您想在 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);
        }
        

        【讨论】:

          【解决方案7】:

          这可以用 lodash _.words:

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

          【讨论】:

            【解决方案8】:

            可以通过split函数完成:

            "This is an amazing sentence.".split(' ')
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2018-07-21
              • 2013-03-20
              • 2020-05-23
              • 1970-01-01
              • 1970-01-01
              • 2016-04-08
              • 1970-01-01
              相关资源
              最近更新 更多