- 匹配最后两位替换为00
let str= "123456" ;
str.replace(/\d{2}$/, '00'); // 123400
str.replace(/(\d{4})(\d{2})/, '$100'); // 123400
2.手机号中间4为替换为 *
let str ="17700001234"
str.replace(/(\d{3})\d{4}(\d{4})/,'$1***$2') //177****1234
- 匹配手机号
let phone ="17700001234"
let telReg =/^[1][2-9][0-9]{9}$/;
if (telReg.test(phone)) {
alert(true)
}else{
alert(false)
}
4.正整数
let reg = /^[1-9]\d*$/
5.正数负数浮点数
let reg = /^(\-|\+)?\d+(\.\d+)?$/
6.去掉文件后缀
function splitFileName(text) {
var pattern = /\.{1}[a-z]{1,}$/;
if (pattern.exec(text) !== null) {
return (text.slice(0, pattern.exec(text).index));
} else {
return text;
}
}
网友评论