The following is a small article about the incompatibility of startsWith functions in js in any browser. I think this is quite good. Now I will share it with you and give you a reference. Let's take a look at the startsWith function used for js testing. However, it is not available in every browser, So we generally need to rewrite this function, the specific usage can be summarized.
In some browsers, he is undefined, so we can handle it like this,
if (typeof String.prototype.startsWith != 'function') { String.prototype.startsWith = function (prefix){ return this.slice(0, prefix.length) === prefix; };}
This should be placed in the function to be loaded on the page, otherwise it will be difficult.
There is also a direct rewrite method, but I haven't tested it yet. You can test it:
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; }
Some say that there are no startsWith and endWith functions in js, but even if some browsers are not declared, they can still be used, but we still want to rewrite them for compatibility.
if (typeof String.prototype.endsWith != 'function') { String.prototype.endsWith = function(suffix) { return this.indexOf(suffix, this.length - suffix.length) !== -1; };}
Use regular expressions to implement startWith and endWith Functions
String. prototype. startWith = function (str) {var reg = new RegExp ("^" + str); return reg. test (this);} // test OK, use str directly. endWith ("abc") can be called as String. prototype. endWith = function (str) {var reg = new RegExp (str + "$"); return reg. test (this );}
In the above js article, the problem that startsWith functions cannot be compatible with any browser is all the content shared by xiaobian. I hope to give you a reference and support for PHP.
For more information about the incompatibility of startsWith functions in js with any browser, please refer to PHP!