1.常用字符对象方法

var string = "helLo,world";
string.charAt(0);                  //指定位置的字符(h)         
string.charAt(string.length-1);    //最后一个字符
string.charCodeAt(0);              //第一个字符的 Unicode 编码(h是114)       
string.concat('new');              //返回新字符串(helLo,worldnew)  
string.indexOf("w");               //indexOf查找w出现位置
string.lastIndexOf("w");           //lastIndexOf方法反向查找w出现的位置
string.match("world");             //匹配到的字符串(world)
string.match(/l/gi);               //匹配正则,返回数组([l,L,l])
string.search(/l/);                //返回匹配的位置,没找到任何匹配的子串,则返回 -1
string.substring(1,4);             //substring方法截取第2~4个字符
string.slice(1,4);                 //slice方法截取第2~4个字符
string.slice(-3);                  //slice方法截取最后三个字符
string.split(",");                 //split方法分割子串
string.replace("h","H");           //replace方法替换
string.toUpperCase();              //toUpperCase()方法转换为大写
string.toLowerCase();              //toLowerCase()方法转换为小写

//ECMAScript 5去除空白字符串
if(String.prototype.trim){
    String.prototype.trim = function(){
        return this.replace(/^[suFEFFxA0]+|[suFEFFxA0]+$/g, '');
    }
}

//ECMAScript 6的查找片段字符串(String.includes()方法)
if (!String.prototype.includes) {
    String.prototype.includes = function(search, start) {
        'use strict';
        if (typeof start !== 'number') {
            start = 0;
        }

        if (start + search.length > this.length) {
            return false;
        } else {
            return this.indexOf(search, start) !== -1;
        }
    };
}
原文地址:https://www.cnblogs.com/alantao/p/5819663.html