split-join-and-replace

  • split

split是可以拆分,和rubysplit类似.

let a = "1,2,3"
a = a.split(",")
=> result a was `["1", "2", "3"]`
  • join

join是可以组合成字符串.也类似于ruby.

let a = [1,2,3]
a = a.join(",")
=> result a was `["1", "2", "3"]`
  • replace

这东西可以替换.替代joinsplit的组合.

join and split example.

let a = "my-name-was-kimi"
a = a.split("-").join("")

等同于下面这种情况.

replace example.

let a = "my-name-was-kimi"
a = a.replace(/-/g, "")

可以发现replace后面有个g,你可以尝试移除g,看看有什么不同.其实g就是全局替换.