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

前端 未结 2 1311
伪装坚强ぢ
伪装坚强ぢ 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:50
    var res = yourString.replace(/[A-Z]/g, "-$&").toLowerCase(); 
    
    0 讨论(0)
  • 2021-01-06 04:55

    Try the following:

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

    0 讨论(0)
提交回复
热议问题