- 手机号正则
/^1[345678]\d{9}$/
- 手机号格式化:
'12345678901'.replace(/\B(?=(?:\d{4})+$)/g, '-')
// '123-4567-8901'
- 价格格式化:
'99893'.replace(/\B(?=(?:\d{3})+$)/g, ',')
// '99,893'
- 去除字符串前后空格
' hello word '.replace(/(^\s*)|(\s*$)/g, "")
' hello word '.replace(/^\s+|\s+$/gm, '')
去除所有空格: str = str.replace(/\s*/g,"");
去除两头空格: str = str.replace(/^\s*|\s*$/g,"");
去除左空格: str = str.replace( /^\s*/, “”);
去除右空格: str = str.replace(/(\s*$)/g, "");
- 正则表达式匹配 协议、域名、端口号
var url = "http://192.168.0.61:8080/touch/index.html?game=AB01";
var reg = new RegExp(/(\w+):\/\/([^/:]+)(:\d*)?/)
var result = url.match(reg);
- 去除HTML中的tag标签
function removeHTMLTag(str) {
str = str.replace(/<\/?[^>]*>/g,''); //去除HTML tag
str = str.replace(/[ | ]*\n/g,'\n'); //去除行尾空白
//str = str.replace(/\n[\s| | ]*\r/g,'\n'); //去除多余空行
str=str.replace(/ /ig,'');//去掉
return str;
}
- 只支持中文或英文
/^([\u4E00-\u9FA5])+$|^([a-zA-Z])+$/.test('中')
- 获取url中的文件
var reg = /.*\/(.*\.html).*/;
var url = 'http://www.baidu.com/a.html?b=3'
console.log(url.match(reg)[1]) //a.html
var reg = /.*\/(.*)\.\w+.*/;
var url = 'http://www.baidu.com/ddddd.d.a.mp4?b=3';
console.log(url.match(reg)[1]) //a.mp4
例:/upload/activity/xueba/20161025/091909_ce15d6bf-1089-4002-9ff6-016e13e420e5.gif
1,获取图片名称
正则表达式:[^\/\\]+$
匹配结果:091909_ce15d6bf-1089-4002-9ff6-016e13e420e5.gif
2,获取后缀
正则表达式:[^\.]\w*$
匹配结果:gif
- 获取日期中的时间
var chstime = "2018-04-08(1:00-19:00)";
var reg = /(0?[0-9]|1[0-9]|2[0-3]):(0?[0-9]|[1-5][0-9])/g;
var reg1 = /[0-9]{1,2}:[0-9]{2}/g
console.log(chstime.match(reg))
console.log(chstime.match(reg1))
- 网址验证
var reg = /^((https|http|ftp|rtsp|mms)?:\/\/)/;
reg.test('http://www.baidu.com') // true
reg.test('/system') // false
-
emoj表情符号
let regexp = /\ud83c[\udf00-\udfff]|\ud83d[\udc00-\ude4f]|\ud83d[\ude80-\udeff]/g;
-
匹配多个中的一个
var arr = ["common","dealer","dealers","order","system","user"]; var reg = /^\/((common)|(dealer)|(dealers)|(order)|(system)|(user))/; var reg1 = new RegExp(`^\/((${arr.join(")|(")}))`); console.log(reg1) console.log(reg1.test("/order/sssss"),'ssss'); console.log(reg.test("/order/b")) console.log(reg.test("/a/order/order")) console.log(reg.test("/a/order/dealers/system"))
网友评论