Javascript/jQuery: Split camelcase string and add hyphen rather than space

前端 未结 5 1265
故里飘歌
故里飘歌 2020-12-23 11:29

I would imagine this is a multiple part situation with regex, but how would you split a camelcase string at the capital letters turning them in to lowercase letters, and the

5条回答
  •  渐次进展
    2020-12-23 11:51

    Try the following:

    var token = document.getElementsByTagName('strong')[0].innerHTML,
        replaced = token.replace(/[a-z][A-Z]/g, function(str, offset) {
           return str[0] + '-' + str[1].toLowerCase();
        });
    
    alert(replaced);
    

    Example - http://jsfiddle.net/7DV6A/2/

    Documentation for the string replace function:

    https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace

提交回复
热议问题