doomi-helper
Version:
Doomisoft NodeJs Common Utilities
47 lines (39 loc) • 1.3 kB
JavaScript
/**
* 字符串处理的工具类
*/
module.exports.endsWith =function(str,suffix){
return (str.substr(str.length - suffix.length) === suffix);
}
module.exports.startWith=function(str,prefix){
return (str.substr(0, prefix.length) === prefix);
}
module.exports.trim =function(str){
return str.replace(/^\s+|\s+$/g, '');
}
module.exports.trimEnd =function(str){
return str.replace(/\s+$/, '');
}
module.exports.trimStart =function(str){
return str.replace(/^\s+/, '');
}
module.exports.replace = function(str, replFrom, replTo, ingoreCase = true) {
// let regex = new RegExp(replFrom, "i", "g");
// return originalStr.replace(regex, replTo);
if (!RegExp.prototype.isPrototypeOf(replFrom)) {
return str.replace(new RegExp(replFrom, (ingoreCase ? "gi" : "g")), replTo);
}
return str.replace(replFrom, replTo);
}
String.prototype.format =function(args){
var _dic = typeof args === "object" ? args :arguments;
return this.replace(/\{([^{}]+)\}/g, function(item, key) {
return _dic.hasOwnProperty(key) ? _dic[key] : item;
});
}
module.exports.format =function(str,args){
let arg = [];
if (arguments.length>1){
for(var i=1;i<arguments.length;i++) arg.push(arguments[i]);
}
return str.format(arg);
}