判断字符串sContent是否以指定的后缀sEnd结尾
function endWith(sContent, sEnd) {
var endReg = new RegExp(".*(?<=" + sEnd + ")$", "i");
return endReg.test(sContent);
}
console.log(endWith("abcsdfffabc", "abc")); // true
判断字符串sContent是否以指定的前缀sStart开头
function startWith(sContent, sStart) {
var startReg = new RegExp("^(?=" + sStart + ").*", "i");
return startReg.test(sContent);
}
console.log(startWith("abcffdfabc", "abc")); // true
判断字符串出现的次数
function sCount(sContent, sMatch) {
var matchReg = new RegExp(sMatch, "ig");
return sContent.match(matchReg) ? sContent.match(matchReg).length : 0;
}
console.log(sCount("asdffdabcsfabffabc", "abc"));
手机号掩码函数
function encryptPhoneNumber(phoneNumber) {
return phoneNumber.replace(/(\d{3})(\d{4})(\d{4})/, '$1****$3')
}
几个 ?
元字符用法
(?:str) 非捕获组
(?=str) 肯定式向前查找
(?!str) 否定式向前查找
(?<=str) 肯定式向后查找
(?<!str) 否定式向后查找
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。