前端操作字符串相关的方法
/**
* 判断字符串以str开头
* @param str 判断的字符
* @returns {boolean}
* @example
* // true
* ',one,two'.startsWith(',');
*/
String.prototype.startsWith = String.prototype.startsWith || function (str) {
if (str === undefined || str === null) return false;
str = str.replace('*', '\\*')
.replace('.', '\\.')
.replace('+', '\\+');
return str === '' || new RegExp('^' + str, 'g').test(this);
};
/**
* 判断字符串以str结尾
*
* @param str 判断的字符
* @returns {boolean}
* @example
* // true
* 'one,two,'.endsWith(',');
*/
String.prototype.endsWith = String.prototype.endsWith || function (str) {
if (str === undefined || str === null) return false;
str = str.replace('*', '\\*')
.replace('.', '\\.')
.replace('+', '\\+');
return str === '' || new RegExp(str + '$', 'g').test(this);
};