How to convert a camel-case string to dashes in JavaScript?

前端 未结 7 1270
情深已故
情深已故 2021-02-13 02:22

I want to convert these strings:

fooBar
FooBar

into:

foo-bar
-foo-bar

How would I do this in JavaScript the m

7条回答
  •  暖寄归人
    2021-02-13 03:14

    For those who do not need the preceding hyphen:

    console.log ("CamelCase".replace(/[A-Z]/g, (match, offset) => (offset > 0 ? '-' : '') + match.toLowerCase()))

提交回复
热议问题