将字符串转换为 spinal case。Spinal case 是 all-lowercase-words-joined-by-dashes 这种形式的,也就是以连字符连接所有小写单词。
这是一些对你有帮助的资源:
之前卡在这道题上了,现在搜到一个答案,先来充数
function spinalCase(str) { return str.replace(/_/g," ").replace(/([A-Z])/g," $1").replace(/^s/,"").replace(/s+/g,"-").toLowerCase(); }
方法二:
function spinalCase(str) { // "It's such a fine line between stupid, and clever." // --David St. Hubbins var reg = /s|\_/g; var str = str.replace(/([a-z])([A-Z])/g,'$1 $2'); str = str.toLowerCase().replace(reg,'-'); return str; }