Metacharacters | Explanation |
---|---|
\d | Matching numbers |
\D | Matching non-numeric |
\w | Matching numbers, letters, underscores |
\W | Not match any letters, numbers, underscores |
\s | Match whitespace |
\S | Matches any character is not whitespace |
. | In addition to matching a newline any character |
^ | Who at the beginning of matches |
$ | Who match to end |
/ R / n newline
var reg = / \ d / // represents the number string as long as it meets the requirements
var reg = / \ D / // represents a non-numeric string as long as it meets the requirements
var reg = / \ w / // represents a string as long as the digital / letter / underscore to meet the requirements
var reg = / \ W / // that as long as there is non-numeric / non-alphabetic string / non-compliance to underline
var reg = / ^ a / // represents the beginning of a long string to meet the conditions
var reg = / a $ / // represents a long string ends to meet the conditions
var reg = / ^ abc $ / // abc claims string only
var reg = / ^ \ d $ / // only one digit represents
var reg = / ^ a | b $ / // indicates a beginning or ending b
var reg = / ^ \ d {11} $ / // string representing the numbers can only appear 11 times
var reg = / ^ \ d {3,} $ / // represents more than 3 times or 3 times a string of numbers can only appear
var reg = / \ d {3,} / // represents a long string comprising three or more than three figures to meet the requirements of such 'ab123'
var reg = / ^ \ d {3,5} $ / // represents a 3 to 5 times the number string can only occur
var reg = / ^ \ d * $ / // represents 0 to multiple numbers in a string can only occur (or nothing written, or only digital)
var reg = / ^ \ d + $ / // ~ 1 represents a plurality of digital (numeric strings have at least one) of the string can only occur
var reg = /^\d?$/ //表示 字符串中 要么什么都没有,或者 只能出现一个数字
var reg = /^gr[ae]y$/ 或者 var reg = var reg = /^gr(a|e)y$/ //表示 匹配 gray或者grey
var reg = /^[0-9]$/ //匹配从0-9的任意一个数字
var reg = /^[0-9a-z]$/ //匹配从0-9的任意一个数字 或者 a-z的任意一个字母
创建正则的方法
var reg = new RegExp()
var reg = /字符/
test()方法
正则对象.test(字符串) 如果字符串符合正则公式,就返回true,否则返回false
限定符 | 说明 |
---|---|
* | 重复零次或更多次 |
+ | 重复一次或更多次 |
? | 重复零次或一次 |
{n} | 代表前面的字符可以出现n次 |
{n,} | 出现n次或更多次 |
{n,m} | 出现n到m次 |
- [] 字符串用中括号括起来,表示匹配其中的任意一个字符,相当于或的意思([]占一个字符的位置)
- [^] 匹配除中括号以内的内容(占一个位置,不能出现[]中的任意一个字符)
- \ 转义符 (把特殊字符转义为普通字符 如 \.)
- | 或者,选择两者中的一个。注意|将左右两边分为两部分,而不管左右两边有多长多乱(会把正则分为两部分)
- () 从两个直接量中选择一个,分组
- eg:gr(a|e)y匹配gray和grey
- [\u4e00-\u9fa5] 匹配汉字
<script>
// 验证手机号: /^\d{11}$/
// 验证邮编: /^\d{6}$/
// 匹配 138 139 186 189 170 152 135 188开头的手机号
var reg = /^1[3578][896025]\d{8}$/
// 邮箱 [email protected]/cn
var reg = /^\w+@\w+\.(com|cn)$/
</script>
标志 | 说明 |
---|---|
i | 忽略大小写 |
g | 全局匹配 |
gi | 全局匹配+忽略大小写 |
//01匹配:正则对象.test(字符串) 输出true和false
//02提取:字符串.match(正则对象);
// 提取字符串中的数字部分
var str = "张三:1000,李四:5000,王五:8000。";
var reg = /\d+/g //g
console.log(str.match(reg)) // ["1000", "5000", "8000"]
//03替换:字符串.replace(正则/被替换的字符,替换的字符)
// 把字符中的,,替换为|
var str = 'abc,efg,123,abc,123,a'
var reg = /,|,/g
console.log( str.replace(reg,'|') )
</script>
// 2. 分组
// 数字3位.数字3位.数字3位.数字3位.数字3位
var reg = /^\d{3}\.\d{3}\.\d{3}\.\d{3}\.\d{3}$/;
var reg = /^\d{3}(\.\d{3}){4}$/
console.log(reg.test('123.123.123.123.123'));
var dateStr = '2016-1-5';
// 正则表达式中的()作为分组来使用,获取分组匹配到的结果用RegExp.$1 $2 $3....来获取
var reg = /(\d{4})-(\d{1,2})-(\d{1,2})/;
if (reg.test(dateStr)) {
console.log(RegExp.$1); // 2016
console.log(RegExp.$2); // 1
console.log(RegExp.$3); // 5
}