Home > Web Front-end > JS Tutorial > Several implementation methods of startWith and endWith in javascript_javascript skills

Several implementation methods of startWith and endWith in javascript_javascript skills

WBOY
Release: 2016-05-16 17:34:36
Original
2175 people have browsed it

JavaScript uses 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); 
}

String .prototype.endWith=function(str){ 
var reg=new RegExp(str "$"); 
return reg.test(this); 
}

JavaScript Implement startWith and endWith effect functions
Copy code The code is as follows:



//The following is an example of usage
var url = location.href;
if (url.startWith('http:// www.jb51.net'))
{
//If the current url starts with http://www.jb51.net/
}

Another one is It is implemented using indexOf:
Copy the code The code is as follows:

var index = str.indexOf( 'abc');

if(index==0){

//Start with 'abc'

}

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template