这两个方法主要用于对字符串的开头
和结尾
进行判断筛选
。
startWith() 使用举例:
let str = 'http://1234.com';
if(str.startsWith('http://')){
alert('普通网址');
} else if(str.startsWith('https://')){
alert('加密网址');
} else if(str.startsWith('svn://')){
alert('svn仓库网址');
} else {
alert('其他');
}
endsWith() 使用举例
let str = 'te.txt';
if(str.endsWith('.txt')){
alert('文本文件');
} else if(str.endsWith('.jpg')){
alert('图片文件');
} else{
alert('其他');
}