Home >Web Front-end >JS Tutorial >Two methods of javascript customizing startWith() and endWith()_javascript skills

Two methods of javascript customizing startWith() and endWith()_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:16:121731browse

1. Use regular expressions to implement startWith and endWith effect functions

Copy code The code is as follows:

String.prototype.startWith=function(str){
var reg=new RegExp("^" str);
return reg.test(this);
}
//Test ok, directly Just use str.endWith("abc") to call
String.prototype.endWith=function(str){
var reg=new RegExp(str "$");
return reg.test( this);
}

2. JavaScript implements startWith and endWith effect functions
Copy code The code is as follows: