【问题标题】:Split String Into Array Elements Based On Punctuation In String - JavaScript基于字符串中的标点符号将字符串拆分为数组元素 - JavaScript
【发布时间】:2019-09-11 05:45:29
【问题描述】:

我想将一个字符串按句子拆分成单独的数组元素。因此,每个周期之后都需要进行拆分。

即这个:

Love is a burning thing and it makes a fiery ring.
Bound by wild desire, I fell in to a ring of fire.

会变成这样:

[ 'Love is a burning thing and it makes a fiery ring.',
 'Bound by wild desire, I fell into a ring of fire', ]

Array[0] 在这种情况下将是"Love is a burning thing and it makes a fiery ring."

我试过了:

function splitLyrics(string) {
  let newArray = [];

  for (let i = 0; i < string.length; i++) {
    let character = string[i];
    let sentence = "";

    if (character !== ".") {
      sentence.concat(character);
    }

    newArray.push(sentence);

  }

  return newArray;

}

console.log(splitLyrics("Love is a burning thing and it makes a fiery ring. Bound by wild desire, I fell in to a ring of fire."));

如您所见,我的问题是我不知道如何将单个字符元素组合成句子元素,并在有句点时分隔。

按照我现有的逻辑,有哪些功能可以实现我正在寻找的功能?

https://www.codewars.com/kata/99-bottles-of-beer-1/train/javascript

【问题讨论】:

    标签: javascript arrays list object


    【解决方案1】:

    使用拆分功能:)。

    string.split(". ");
    

    您可以将它与 trim() 结合使用,它可以像这样删除字符周围的空格(仅字符串的开头和结尾):

    const stringArray = string
      .split(".")
      .map(str => str.trim())
    

    每个字符串首先在 '.' 上拆分,然后对每个字符串进行修剪。 这应该使代码对于奇怪的空白问题更加健壮。

    如果您需要更多选项,我推荐 match/regexp :)

    【讨论】:

    • 哇哦——太简单了!我没有意识到你可以用这样的标点符号来分割一个字符串。谢谢!
    • 不幸的是,split 会从句子中删除结尾的 .
    • 是的,user2340824 是对的。 split 是字符串 -> 数组的“第一个 goto”,但如果您需要更多选项,请查看正则表达式匹配(其他答案:))
    • 嗯我的控制台不喜欢它末尾的.trim()....TypeError: string.split(...).trim is not a function
    【解决方案2】:

    您可以使用正则表达式和 .match 来代替 - 匹配一个非空格字符,然后是非句点字符,然后是句点字符:

    const splitLyrics = str => str.match(/\S[^.]*\./g);
    
    console.log(splitLyrics("Love is a burning thing and it makes a fiery ring. Bound by wild desire, I fell in to a ring of fire."));

    这也很容易扩展到其他句子终止标点符号,例如感叹号和问号:

    const splitLyrics = str => str.match(/\S[^.?!]*[.?!]/g);
    
    console.log(splitLyrics("Love is a burning thing and it makes a fiery ring. Bound by wild desire, I fell in to a ring of fire. Foo bar! Bar baz? Buzz."));

    如果需要var song = sing();,并且测试人员继续调用song,那么您应该编写一个返回函数的函数,尽管这似乎是一件很奇怪的事情:

    const sing = () => str => str.match(/\S[^.]*\./g);
    const song = sing();
    console.log(song("Love is a burning thing and it makes a fiery ring. Bound by wild desire, I fell in to a ring of fire."));

    kata 似乎希望您粘贴整首歌曲的歌词,然后返回相应的行:

    你可以在这里找到完整的歌词。

    所以要么这样做:

    // snippet isn't actually runnable, but it's too much text to paste in the answer proper
    
    const sing = () => `99 bottles of beer on the wall, 99 bottles of beer.
    Take one down and pass it around, 98 bottles of beer on the wall.
    
    98 bottles of beer on the wall, 98 bottles of beer.
    Take one down and pass it around, 97 bottles of beer on the wall.
    
    97 bottles of beer on the wall, 97 bottles of beer.
    Take one down and pass it around, 96 bottles of beer on the wall.
    
    96 bottles of beer on the wall, 96 bottles of beer.
    Take one down and pass it around, 95 bottles of beer on the wall.
    
    95 bottles of beer on the wall, 95 bottles of beer.
    Take one down and pass it around, 94 bottles of beer on the wall.
    
    94 bottles of beer on the wall, 94 bottles of beer.
    Take one down and pass it around, 93 bottles of beer on the wall.
    
    93 bottles of beer on the wall, 93 bottles of beer.
    Take one down and pass it around, 92 bottles of beer on the wall.
    
    92 bottles of beer on the wall, 92 bottles of beer.
    Take one down and pass it around, 91 bottles of beer on the wall.
    
    91 bottles of beer on the wall, 91 bottles of beer.
    Take one down and pass it around, 90 bottles of beer on the wall.
    
    90 bottles of beer on the wall, 90 bottles of beer.
    Take one down and pass it around, 89 bottles of beer on the wall.
    
    89 bottles of beer on the wall, 89 bottles of beer.
    Take one down and pass it around, 88 bottles of beer on the wall.
    
    88 bottles of beer on the wall, 88 bottles of beer.
    Take one down and pass it around, 87 bottles of beer on the wall.
    
    87 bottles of beer on the wall, 87 bottles of beer.
    Take one down and pass it around, 86 bottles of beer on the wall.
    
    86 bottles of beer on the wall, 86 bottles of beer.
    Take one down and pass it around, 85 bottles of beer on the wall.
    
    85 bottles of beer on the wall, 85 bottles of beer.
    Take one down and pass it around, 84 bottles of beer on the wall.
    
    84 bottles of beer on the wall, 84 bottles of beer.
    Take one down and pass it around, 83 bottles of beer on the wall.
    
    83 bottles of beer on the wall, 83 bottles of beer.
    Take one down and pass it around, 82 bottles of beer on the wall.
    
    82 bottles of beer on the wall, 82 bottles of beer.
    Take one down and pass it around, 81 bottles of beer on the wall.
    
    81 bottles of beer on the wall, 81 bottles of beer.
    Take one down and pass it around, 80 bottles of beer on the wall.
    
    80 bottles of beer on the wall, 80 bottles of beer.
    Take one down and pass it around, 79 bottles of beer on the wall.
    
    79 bottles of beer on the wall, 79 bottles of beer.
    Take one down and pass it around, 78 bottles of beer on the wall.
    
    78 bottles of beer on the wall, 78 bottles of beer.
    Take one down and pass it around, 77 bottles of beer on the wall.
    
    77 bottles of beer on the wall, 77 bottles of beer.
    Take one down and pass it around, 76 bottles of beer on the wall.
    
    76 bottles of beer on the wall, 76 bottles of beer.
    Take one down and pass it around, 75 bottles of beer on the wall.
    
    75 bottles of beer on the wall, 75 bottles of beer.
    Take one down and pass it around, 74 bottles of beer on the wall.
    
    74 bottles of beer on the wall, 74 bottles of beer.
    Take one down and pass it around, 73 bottles of beer on the wall.
    
    73 bottles of beer on the wall, 73 bottles of beer.
    Take one down and pass it around, 72 bottles of beer on the wall.
    
    72 bottles of beer on the wall, 72 bottles of beer.
    Take one down and pass it around, 71 bottles of beer on the wall.
    
    71 bottles of beer on the wall, 71 bottles of beer.
    Take one down and pass it around, 70 bottles of beer on the wall.
    
    70 bottles of beer on the wall, 70 bottles of beer.
    Take one down and pass it around, 69 bottles of beer on the wall.
    
    69 bottles of beer on the wall, 69 bottles of beer.
    Take one down and pass it around, 68 bottles of beer on the wall.
    
    68 bottles of beer on the wall, 68 bottles of beer.
    Take one down and pass it around, 67 bottles of beer on the wall.
    
    67 bottles of beer on the wall, 67 bottles of beer.
    Take one down and pass it around, 66 bottles of beer on the wall.
    
    66 bottles of beer on the wall, 66 bottles of beer.
    Take one down and pass it around, 65 bottles of beer on the wall.
    
    65 bottles of beer on the wall, 65 bottles of beer.
    Take one down and pass it around, 64 bottles of beer on the wall.
    
    64 bottles of beer on the wall, 64 bottles of beer.
    Take one down and pass it around, 63 bottles of beer on the wall.
    
    63 bottles of beer on the wall, 63 bottles of beer.
    Take one down and pass it around, 62 bottles of beer on the wall.
    
    62 bottles of beer on the wall, 62 bottles of beer.
    Take one down and pass it around, 61 bottles of beer on the wall.
    
    61 bottles of beer on the wall, 61 bottles of beer.
    Take one down and pass it around, 60 bottles of beer on the wall.
    
    60 bottles of beer on the wall, 60 bottles of beer.
    Take one down and pass it around, 59 bottles of beer on the wall.
    
    59 bottles of beer on the wall, 59 bottles of beer.
    Take one down and pass it around, 58 bottles of beer on the wall.
    
    58 bottles of beer on the wall, 58 bottles of beer.
    Take one down and pass it around, 57 bottles of beer on the wall.
    
    57 bottles of beer on the wall, 57 bottles of beer.
    Take one down and pass it around, 56 bottles of beer on the wall.
    
    56 bottles of beer on the wall, 56 bottles of beer.
    Take one down and pass it around, 55 bottles of beer on the wall.
    
    55 bottles of beer on the wall, 55 bottles of beer.
    Take one down and pass it around, 54 bottles of beer on the wall.
    
    54 bottles of beer on the wall, 54 bottles of beer.
    Take one down and pass it around, 53 bottles of beer on the wall.
    
    53 bottles of beer on the wall, 53 bottles of beer.
    Take one down and pass it around, 52 bottles of beer on the wall.
    
    52 bottles of beer on the wall, 52 bottles of beer.
    Take one down and pass it around, 51 bottles of beer on the wall.
    
    51 bottles of beer on the wall, 51 bottles of beer.
    Take one down and pass it around, 50 bottles of beer on the wall.
    
    50 bottles of beer on the wall, 50 bottles of beer.
    Take one down and pass it around, 49 bottles of beer on the wall.
    
    49 bottles of beer on the wall, 49 bottles of beer.
    Take one down and pass it around, 48 bottles of beer on the wall.
    
    48 bottles of beer on the wall, 48 bottles of beer.
    Take one down and pass it around, 47 bottles of beer on the wall.
    
    47 bottles of beer on the wall, 47 bottles of beer.
    Take one down and pass it around, 46 bottles of beer on the wall.
    
    46 bottles of beer on the wall, 46 bottles of beer.
    Take one down and pass it around, 45 bottles of beer on the wall.
    
    45 bottles of beer on the wall, 45 bottles of beer.
    Take one down and pass it around, 44 bottles of beer on the wall.
    
    44 bottles of beer on the wall, 44 bottles of beer.
    Take one down and pass it around, 43 bottles of beer on the wall.
    
    43 bottles of beer on the wall, 43 bottles of beer.
    Take one down and pass it around, 42 bottles of beer on the wall.
    
    42 bottles of beer on the wall, 42 bottles of beer.
    Take one down and pass it around, 41 bottles of beer on the wall.
    
    41 bottles of beer on the wall, 41 bottles of beer.
    Take one down and pass it around, 40 bottles of beer on the wall.
    
    40 bottles of beer on the wall, 40 bottles of beer.
    Take one down and pass it around, 39 bottles of beer on the wall.
    
    39 bottles of beer on the wall, 39 bottles of beer.
    Take one down and pass it around, 38 bottles of beer on the wall.
    
    38 bottles of beer on the wall, 38 bottles of beer.
    Take one down and pass it around, 37 bottles of beer on the wall.
    
    37 bottles of beer on the wall, 37 bottles of beer.
    Take one down and pass it around, 36 bottles of beer on the wall.
    
    36 bottles of beer on the wall, 36 bottles of beer.
    Take one down and pass it around, 35 bottles of beer on the wall.
    
    35 bottles of beer on the wall, 35 bottles of beer.
    Take one down and pass it around, 34 bottles of beer on the wall.
    
    34 bottles of beer on the wall, 34 bottles of beer.
    Take one down and pass it around, 33 bottles of beer on the wall.
    
    33 bottles of beer on the wall, 33 bottles of beer.
    Take one down and pass it around, 32 bottles of beer on the wall.
    
    32 bottles of beer on the wall, 32 bottles of beer.
    Take one down and pass it around, 31 bottles of beer on the wall.
    
    31 bottles of beer on the wall, 31 bottles of beer.
    Take one down and pass it around, 30 bottles of beer on the wall.
    
    30 bottles of beer on the wall, 30 bottles of beer.
    Take one down and pass it around, 29 bottles of beer on the wall.
    
    29 bottles of beer on the wall, 29 bottles of beer.
    Take one down and pass it around, 28 bottles of beer on the wall.
    
    28 bottles of beer on the wall, 28 bottles of beer.
    Take one down and pass it around, 27 bottles of beer on the wall.
    
    27 bottles of beer on the wall, 27 bottles of beer.
    Take one down and pass it around, 26 bottles of beer on the wall.
    
    26 bottles of beer on the wall, 26 bottles of beer.
    Take one down and pass it around, 25 bottles of beer on the wall.
    
    25 bottles of beer on the wall, 25 bottles of beer.
    Take one down and pass it around, 24 bottles of beer on the wall.
    
    24 bottles of beer on the wall, 24 bottles of beer.
    Take one down and pass it around, 23 bottles of beer on the wall.
    
    23 bottles of beer on the wall, 23 bottles of beer.
    Take one down and pass it around, 22 bottles of beer on the wall.
    
    22 bottles of beer on the wall, 22 bottles of beer.
    Take one down and pass it around, 21 bottles of beer on the wall.
    
    21 bottles of beer on the wall, 21 bottles of beer.
    Take one down and pass it around, 20 bottles of beer on the wall.
    
    20 bottles of beer on the wall, 20 bottles of beer.
    Take one down and pass it around, 19 bottles of beer on the wall.
    
    19 bottles of beer on the wall, 19 bottles of beer.
    Take one down and pass it around, 18 bottles of beer on the wall.
    
    18 bottles of beer on the wall, 18 bottles of beer.
    Take one down and pass it around, 17 bottles of beer on the wall.
    
    17 bottles of beer on the wall, 17 bottles of beer.
    Take one down and pass it around, 16 bottles of beer on the wall.
    
    16 bottles of beer on the wall, 16 bottles of beer.
    Take one down and pass it around, 15 bottles of beer on the wall.
    
    15 bottles of beer on the wall, 15 bottles of beer.
    Take one down and pass it around, 14 bottles of beer on the wall.
    
    14 bottles of beer on the wall, 14 bottles of beer.
    Take one down and pass it around, 13 bottles of beer on the wall.
    
    13 bottles of beer on the wall, 13 bottles of beer.
    Take one down and pass it around, 12 bottles of beer on the wall.
    
    12 bottles of beer on the wall, 12 bottles of beer.
    Take one down and pass it around, 11 bottles of beer on the wall.
    
    11 bottles of beer on the wall, 11 bottles of beer.
    Take one down and pass it around, 10 bottles of beer on the wall.
    
    10 bottles of beer on the wall, 10 bottles of beer.
    Take one down and pass it around, 9 bottles of beer on the wall.
    
    9 bottles of beer on the wall, 9 bottles of beer.
    Take one down and pass it around, 8 bottles of beer on the wall.
    
    8 bottles of beer on the wall, 8 bottles of beer.
    Take one down and pass it around, 7 bottles of beer on the wall.
    
    7 bottles of beer on the wall, 7 bottles of beer.
    Take one down and pass it around, 6 bottles of beer on the wall.
    
    6 bottles of beer on the wall, 6 bottles of beer.
    Take one down and pass it around, 5 bottles of beer on the wall.
    
    5 bottles of beer on the wall, 5 bottles of beer.
    Take one down and pass it around, 4 bottles of beer on the wall.
    
    4 bottles of beer on the wall, 4 bottles of beer.
    Take one down and pass it around, 3 bottles of beer on the wall.
    
    3 bottles of beer on the wall, 3 bottles of beer.
    Take one down and pass it around, 2 bottles of beer on the wall.
    
    2 bottles of beer on the wall, 2 bottles of beer.
    Take one down and pass it around, 1 bottle of beer on the wall.
    
    1 bottle of beer on the wall, 1 bottle of beer.
    Take one down and pass it around, no more bottles of beer on the wall.
    
    No more bottles of beer on the wall, no more bottles of beer. 
    Go to the store and buy some more, 99 bottles of beer on the wall.`.split('\n').filter(Boolean)

    或者,对于这首特别的“歌曲”,它非常重复,你可以自己创作歌词,这可能是练习的重点,虽然不是很清楚:

    const sing = () => [
      ...Array.from(
        { length: 98 },
        (_, i) => [
          `${99 - i} bottles of beer on the wall, ${99 - i} bottles of beer.`,
          `Take one down and pass it around, ${99 - i - 1} bottles of beer on the wall.`
        ]
      ).flat(),
      `1 bottle of beer on the wall, 1 bottle of beer.`,
      `Take one down and pass it around, no more bottles of beer on the wall.`,
      `No more bottles of beer on the wall, no more bottles of beer.`,
      `Go to the store and buy some more, 99 bottles of beer on the wall.`
    ];
    const song = sing();
    console.log(song[0] === '99 bottles of beer on the wall, 99 bottles of beer.');

    【讨论】:

    • 在我的 IDE function splitLyrics (string) { return string.match(/\S[^.?!]*[.?!]/g); } 中执行此操作似乎在我的终端中有效,但在 codewars 中无效:codewars.com/kata/99-bottles-of-beer-1/train/javascriptTypeError: Cannot read property 'match' of undefined,这对我来说没有意义。
    • 听起来您没有将任何参数传递给splitLyrics,这就是string 将是undefined 的唯一原因。在答案中按“运行代码 sn-p” - 您会看到代码确实运行并且结果符合预期
    • CodeWars中设置的函数最初是不带参数的? i.imgur.com/cXHriJU.png, i.imgur.com/KNTqqvD.png
    • 在您的测试的第一行,您正在调用sing,而您的var song = sing() 没有参数,这意味着sing 的第一个参数是undefined。 (我也不明白那条线应该做什么)
    • 如果需要var song = sing();,并且测试人员继续调用song,那么您应该编写一个返回函数的函数,尽管这看起来很奇怪要做的事。 const sing = () =&gt; str =&gt; str.match(/\S[^.]*\./g);,见编辑
    猜你喜欢
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 2011-05-31
    相关资源
    最近更新 更多