How to change camelCase to slug-case (or kabob-case) via regex in JavaScript

前端 未结 2 1323
伪装坚强ぢ
伪装坚强ぢ 2021-01-06 04:24

For some reason, this answer I found for (supposedly) how to do it in php just gives me the wrong matches. It seems to add the dash, but also replace the capital letter wit

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-06 04:55

    Try the following:

    var result = "fooBarBaz".replace(/([A-Z])/g, "-$1").toLowerCase(); 
    console.log(result);

提交回复
热议问题