【问题标题】:How to split array elements by special characters [closed]如何通过特殊字符分割数组元素[关闭]
【发布时间】:2020-10-26 07:13:56
【问题描述】:

我有一个这样的字符串数组:

var strings = ['elephant-rides', 'are', 'fun!'];

我怎样才能达到下面给出的结果?

var result = ['elephant', '-', 'rides', 'are', 'fun', '!'];

【问题讨论】:

标签: javascript split


【解决方案1】:

您可以在平面数组中匹配单词或非单词字符。

var strings = ['elephant-rides', 'are', 'fun!'],
    result = strings.flatMap(s => s.match(/\w+|\W+/g));

console.log(result);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-03
    • 2013-08-05
    • 2017-08-14
    • 1970-01-01
    • 1970-01-01
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多