我如何分裂一个字符串与多个分隔符在JavaScript?

我试图拆分逗号和空格,但AFAIK JavaScript的split()函数只支持一个分隔符。


当前回答

我用缩减和过滤解决了这个问题。它可能不是最易读的解决方案,也不是最快的,在现实生活中,我可能会在这里使用aaron的答案,但写起来很有趣。

[' ','_','-','.',',',':','@'].reduce(
(segs, sep) => segs.reduce(
(out, seg) => out.concat(seg.split(sep)), []), 
['E-mail Address: user@domain.com, Phone Number: +1-800-555-0011']
).filter(x => x)

或作为一个函数:

function msplit(str, seps) {
  return seps.reduce((segs, sep) => segs.reduce(
    (out, seg) => out.concat(seg.split(sep)), []
  ), [str]).filter(x => x);
}

这将输出:

['E','mail','Address','user','domain','com','0','Phone','Number','+1','800','555','0011']

如果没有最后的过滤器,你将在数组中得到空字符串,其中两个不同的分隔符彼此相邻。

其他回答

让我们保持简单:(在你的RegEx中添加“[]+”表示“1或更多”)

这意味着“+”和“{1,}”是相同的。

var words = text.split(/[ .:;?!~,`"&|()<>{}\[\]\r\n/\\]+/); // note ' and - are kept

例如,如果你在String 07:05:45PM中使用了split and replace

var hour = time.replace("PM", "").split(":");

结果

[ '07', '05', '45' ]

你可以将一个正则表达式传递给JavaScript的split()方法。例如:

"1,2 3".split(/,| /) 
["1", "2", "3"]

或者,如果你想让多个分隔符一起只起到一个作用:

"1, 2, , 3".split(/(?:,| )+/) 
["1", "2", "3"]

您必须使用非捕获(?:)括号,因为 否则它会被拼接回结果中。或者你可以聪明一点 比如Aaron,使用一个角色类。

在Safari和Firefox中测试的示例。

从@stephen-sweriduk解决方案开始(这对我来说更有趣!),我对它进行了轻微的修改,使其更加通用和可重用:

/**
 * Adapted from: http://stackoverflow.com/questions/650022/how-do-i-split-a-string-with-multiple-separators-in-javascript
*/
var StringUtils = {

  /**
   * Flatten a list of strings
   * http://rosettacode.org/wiki/Flatten_a_list
   */
  flatten : function(arr) {
    var self=this;
    return arr.reduce(function(acc, val) {
        return acc.concat(val.constructor === Array ? self.flatten(val) : val);
    },[]);
  },

  /**
   * Recursively Traverse a list and apply a function to each item
   * @param list array
   * @param expression Expression to use in func
   * @param func function of (item,expression) to apply expression to item
   *
   */
  traverseListFunc : function(list, expression, index, func) {
    var self=this;
    if(list[index]) {
        if((list.constructor !== String) && (list[index].constructor === String))
            (list[index] != func(list[index], expression)) ? list[index] = func(list[index], expression) : null;
        (list[index].constructor === Array) ? self.traverseListFunc(list[index], expression, 0, func) : null;
        (list.constructor === Array) ? self.traverseListFunc(list, expression, index+1, func) : null;
    }
  },

  /**
   * Recursively map function to string
   * @param string
   * @param expression Expression to apply to func
   * @param function of (item, expressions[i])
   */
  mapFuncToString : function(string, expressions, func) {
    var self=this;
    var list = [string];
    for(var i=0, len=expressions.length; i<len; i++) {
        self.traverseListFunc(list, expressions[i], 0, func);
    }
    return self.flatten(list);
  },

  /**
   * Split a string
   * @param splitters Array of characters to apply the split
   */
  splitString : function(string, splitters) {
    return this.mapFuncToString(string, splitters, function(item, expression) {
      return item.split(expression);
    })
  },

}

然后

var stringToSplit = "people and_other/things";
var splitList = [" ", "_", "/"];
var splittedString=StringUtils.splitString(stringToSplit, splitList);
console.log(splitList, stringToSplit, splittedString);

归还原物的:

[ ' ', '_', '/' ] 'people and_other/things' [ 'people', 'and', 'other', 'things' ]

我在寻找c# string. split()函数的替代品时遇到了这个问题,该函数使用参数中的字符分割字符串。

在JavaScript中,你可以使用map和reduce来迭代分隔字符和中间值:

let splitters = [",", ":", ";"]; // or ",:;".split("");
let start= "a,b;c:d";
let values = splitters.reduce((old, c) => old.map(v => v.split(c)).flat(), [start]);
// values is ["a", "b", "c", "d"]

Flat()用于平滑中间结果,因此每次迭代都处理没有嵌套数组的字符串列表。每次迭代对old中的所有值应用split,然后返回拆分器中要被下一个值拆分的中间结果列表。Reduce()使用包含初始字符串值的数组进行初始化。