First, the use of regular expressions to achieve startwith, endwith effect function
Copy Code code as follows:
String.prototype.startwith=function (str) {
var reg=new RegExp ("^" +str);
Return Reg.test (this);
}
Test OK, directly using Str.endwith ("ABC") Way to call
String.prototype.endwith=function (str) {
var reg=new RegExp (str+ "$");
Return Reg.test (this);
}
Second, JavaScript implementation startwith, Endwith effect function
Copy Code code as follows:
<script type= "Text/javascript" >
string.prototype.endwith= function (s) {
if (s==null| | s== "" | | this.length==0| | S.length>this.length)
return false;
if (this.substring (this.length-s.length) ==s)
return true;
Else
return false;
return true;
}
String.prototype.startwith=function (s) {
if (s==null| | s== "" | | this.length==0| | S.length>this.length)
return false;
if (this.substr (0,s.length) ==s)
return true;
Else
return false;
return true;
}
</script>
//below is using the example
var url = location.href;
if (url.startwith (' http://www.jb51.net '))
{
//If the current URL starts with http://www.jb51.net/
}