How do I split a string with multiple separators in javascript?

前端 未结 22 1571
走了就别回头了
走了就别回头了 2020-11-21 23:14

How do I split a string with multiple separators in JavaScript? I\'m trying to split on both commas and spaces but, AFAIK, JS\'s split function only supports one separator.

22条回答
  •  时光说笑
    2020-11-21 23:39

    You could just lump all the characters you want to use as separators either singularly or collectively into a regular expression and pass them to the split function. For instance you could write:

    console.log( "dasdnk asd, (naks) :d skldma".split(/[ \(,\)]+/) );
    

    And the output will be:

    ["dasdnk", "asd", "naks", ":d", "skldma"]
    

提交回复
热议问题