js正则
2020-05-27 本文已影响0人
simplerandom
// 包含abc且开头
var reg = /^abc/;
console.log(reg.test("abc"));
console.log(reg.test("aabc"));
// 精确匹配abc
var reg = /^abc$/;
console.log(reg.test("abcabc"));
console.log(reg.test("aabc"));
// 单词包含a或b或c
var reg = /[abc]/;
console.log(reg.test("abc"));
console.log(reg.test("aabc"));
// 单词只能是a或b或c
var reg = /^[abc]$/;
console.log(reg.test("ab"));
console.log(reg.test("c"));
// ^a取反
var reg = /^[^a-zA-Z0-9-_]$/;
console.log(reg.test("ab"));
console.log(reg.test("_"));
// 出现abbc,aabbc,abbcc,abbccc,aabbcc,aabbccc
var reg = /^a{1,2}b{2}c{1,3}$/;
console.log(reg.test("aabbc"));
console.log(reg.test("aaabcccc"));
常用
长度为6-8的任意字母或数字
var reg = /^[a-zA-Z0-9-_]{6,8}$/;
console.log(reg.test("kkj125"));
console.log(reg.test("_552"));
console.log(reg.test("_552gfdgdfs"));