JavaScript常用技巧之字符串操作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript常用技巧之字符串操作相关的知识,希望对你有一定的参考价值。

1、首字母大写

str.replace(/\b\w+/g, function (word) {
    return word.substring(0, 1).toLowerCase() + word.substring(1);
});

2、截取字符串最后几位

str.substring(str.length-X)

3、生成随机字符串

Math.random().toString(36).substring(7)

4、解析 Base64

new Buffer(base64str, ‘base64‘).toString();

 

以上是关于JavaScript常用技巧之字符串操作的主要内容,如果未能解决你的问题,请参考以下文章