将段落拆分为数组javascript

时间:2019-03-19 13:47:06

标签: javascript arrays string split

我有如下代码

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);
      });
    }

并获取如下数据

将过滤器在温水浴中浸泡至少五分钟后,将其放入虹吸管顶部组件(或漏斗)的底部,然后钩在漏斗玻璃管的底部。底部组件。,插入漏斗,过滤器和所有组件。

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

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:1)

我相信您想用 dot 后跟逗号来分割字符串:

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

OR::使用RegEx

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