【问题标题】:split paragraph into array javascript将段落拆分为数组 javascript
【发布时间】:2019-03-19 13:47:06
【问题描述】:

我有如下代码

return fetch(URI + 'api/brewing/1')
      .then((response) => response.json())
      .then((responseJson) => {

          var parsedResponse = JSON.parse(responseJson["data"][0]["steps"]);
          var stringData = JSON.stringify(parsedResponse);
          })
      .catch((error) => {
        console.error(error);
      });
    }

并获取如下数据

将过滤器在温水浴中浸泡至少 5 分钟后,将其放入虹吸管顶部组件或料斗的底部,并钩在料斗玻璃管的底部。填充虹吸管底部组件。,插入料斗,过滤器和所有。

我想将 dotcomma 分隔符之后的段落拆分为一个数组,以便循环所有数据。我怎样才能做到这一点?谢谢。

【问题讨论】:

  • 请发布您现有的代码,以便我们帮助您修复它。这不是代码请求服务,因此您需要向我们展示您已经尝试过的内容。
  • str.split(/[.,]+/)
  • String.split() ?作为记录,这是您在每个教程中学习的非常基本的方法。
  • 您尝试过以下建议吗?其中之一完全符合您的要求。

标签: javascript arrays string split


【解决方案1】:

我相信你想用 dot 后跟 comma 分割字符串:

var s = 'After soaking your filter in a warm water bath for at least five minutes, drop it into the bottom of your siphons top component, or hopper, and hook to the bottom of the hoppers glass tubing.,Fill your siphon bottom component.,Insert the hopper, filter and all.'
s = s.split('.,');
console.log(s);

或:使用正则表达式

var s = 'After soaking your filter in a warm water bath for at least five minutes, drop it into the bottom of your siphons top component, or hopper, and hook to the bottom of the hoppers glass tubing.,Fill your siphon bottom component.,Insert the hopper, filter and all.'
s = s.split(/(?:\.\,)/g);
console.log(s);

【讨论】:

    【解决方案2】:

    尝试str.split(/[,.]+/);将点和逗号分隔符后的段落拆分为数组

    let str ="After soaking your filter., in a warm water bath for at least five minutes, drop it into the bottom of your siphons top component, or hopper,. and hook to the bottom of the hoppers glass tubing.,Fill your siphon bottom component.,Insert the hopper, filter and all.";
    
    let splittedArray = str.split(".,");
    
    console.log(splittedArray);

    【讨论】:

    • @Archer 抱歉,现在我有问题了。我已经更新了我的答案。谢谢
    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 2015-07-10
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 1970-01-01
    相关资源
    最近更新 更多