Split string and trim every element

后端 未结 5 942
失恋的感觉
失恋的感觉 2021-02-01 14:39

Is there any library API or regex pattern to split a String on some delimiter and automatically trim leading and trailing spaces from every element without having to loop the el

5条回答
  •  走了就别回头了
    2021-02-01 15:27

    Without regex it should look like this:

    "  A   B #  C#D# E  #  "
        .split('#')
        .map(function(item) { return item.trim(); } )
        .filter(function(n){ return n != "" });
    

    outputs: ["A B", "C", "D", "E"]

提交回复
热议问题