把字符串大小写切换的方法

var str = '$a1a2a_B3B4B$';

function transformCode(code) {
    return code.replace(/([A-Z])|([a-z])/g, function(match, $1, $2) {
        if ($1) {
            return $1.toLowerCase();
        } else if ($2) {
            return $2.toUpperCase();
        }
    });
}
posted @ 2020-03-23 15:04  月半流云  阅读(338)  评论(0编辑  收藏  举报