By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy

Q: Replace kebab case with snake case (dashes with underscore)

+3 votes

I need to create a function in javascript which will replace

kebab case (dashes: "-")


snake cased version (underscore dash "_")

in a string.

asked in JavaScript category by user matthew44

1 Answer

+2 votes

Here you go (with replace and regex):

function kebabToSnake(str) {
    //replace all dashes with
    var myString = str.replace(/-/g, "_");
    //return str
    return myString;
    //try with: "this - is -a - ---test";


answered by user mitko