^以……开头
$以……结尾
var str="18674187236"//严格匹配 ^以……开头 $以……结尾var reg=/^1[3-9]\d{9}$/;console.log(reg.test(str)) //true
例子
清除字符串前后的空格
var str = " hello ";var reg = /^\s+|\s+$/g;// console.log(str.replace(reg,""));//字符串hellovar arr = [];var res = str.replace(reg,"");arr.push(res);console.log(arr);//["hello"]
trim()去除字符串前后的空格
**相当于/^\s+|\s+$/g
var str=" hello ";var arr=[];arr.push(str.trim());console.log(arr); //["hello"]
去除input输入框内输入字符串的空格
var arr=[]var input=document.getElementById("input");input.onkeydown=function(){if(event.keyCode==13){var res=this.value.trim();arr.push(res);// console.log(arr);}console.log(arr);}
注意 \转义字符
转义字符\var a ="hello\"";console.log(a); \\hello"
var str = "https://www.baidu.com"var str2 = "http://www.baidu.com"var reg = /(http|https):\/\/[w]{3}\.baidu\.com/console.log(reg.test(str));console.log(reg.test(str2));
**
