使用node.js实现python中的标准方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_39198406/article/details/87253557
// 去掉两边的空格和空行
function strip(s){
    return stripRight(stripLeft(s));
}
//去掉左边的空格和空行
function stripLeft(s){
    if(s == null) return "";
    return s.replace(/^[\s\t\n\r]+/,"");
}

//去掉右边的空格和空行
function stripRight(s){
    if(s == null) return "";
    return s.replace(/[\s\t\n\r]+$/,"");
}

// 判断结尾的字符串
function endswith(origin, value){
    let real = origin.substr(origin.length - value.length, value.length);
    return real === value
}

// 判断开始的字符串
function startswith(origin, value){
    let real = origin.substr(0, value.length);
    return real === value
}

// 替换所有指定字符串为指定字符串
function replaceAll(string, key, value){
    return string.replace(new RegExp(key,'g'), value);
}

猜你喜欢

转载自blog.csdn.net/weixin_39198406/article/details/87253557