ie8 compatibility issues (endwith, startwith, trim)

The ie8 browser does not support the methods of endsWith, trim (), startsWith, etc., you will encounter compatibility problems in use

Solution:

Rewrite the above method:

1, trim, ltrim or rtrim

Use regular expressions

 String.prototype.trim=function(){
      return this.replace(/(^\s*)|(\s*$)/g, "");
   }
   String.prototype.ltrim=function(){
      return this.replace(/(^\s*)/g,"");
   }
   String.prototype.rtrim=function(){
      return this.replace(/(\s*$)/g,"");
   }

2. StartWith rewrite

String.prototype.startWith=function(str){
if(str==null||str==""||this.length==0||str.length>this.length)
  return false;
if(this.substr(0,str.length)==str)
  return true;
else
  return false;
return true;
}
//或者使用正则表达式,方法如下
String.prototype.startWith = function(str) {
var reg = new RegExp("^" + str);
return reg.test(this);

3. Rewrite endsWith

String.prototype.endWith=function(str){
    if(str==null||str==""||this.length==0||str.length>this.length)
        return false;
    if(this.substring(this.length-str.length)==str)
        return true;
    else
        return false;
    return true;
}
//或者使用正则表达式
String.prototype.endWith = function(str) {
var reg = new RegExp(str + "$");
return reg.test(this);
}

note:

The above method: all put in $ (function () {

The above method

});

Published 26 original articles · praised 0 · visits 9934

Guess you like

Origin blog.csdn.net/weixin_38246518/article/details/79394417