线上测试工具:https://c.runoob.com/front-end/854
示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var str="abc123derf"
var patt1=/[0-9]+/
// var result=str.match(patt1); 123
var result=/^[a-z0-9-_]{3,15}$/
var aaa="hhh哈"
if(aaa.match(result)){
alert(1)
}
var a="ab112cdeaafhAdk.kspodkjSlj f 01j123 58_.123-"
console.log(a.match(/[aeiou]/g)) //aeaao 全局匹配aeiou
console.log(a.match(/[^aeiou]/g)) //bcdfh..... 全局匹配非aeiou
console.log(a.match(/[A-Z]/g)) //A S 匹配大写字母 [A-Z]表示一个区间
console.log(a.match(/[\s\S]/g)) //匹配所有 \s 是匹配所有空白符,包括换行,\S 非空白符,不包括换行。
console.log(a.match(/[\w]/g)) //匹配字母、数字、下划线。等价于 [A-Za-z0-9_]
console.log(a.match(/[1-9][0-9]*/g)) //匹配一个正整数,[1-9]设置第一个数字不是 0,[0-9]* 表示任意多个数字
//用圆括号 () 将所有选择项括起来,相邻的选择项之间用 | 分隔。
//() 表示捕获分组,() 会把每个分组里的匹配的值保存起来
//但用圆括号会有一个副作用,使相关的匹配会被缓存,此时可用 ?: 放在第一个选项前来消除这种副作用。
console.log(a.match(/([1-9])([a-z]+)/g)) //匹配以一个数字开头,多个字母结尾
console.log(a.match(/[0-9\.\-]{1,}/g)) 匹配所有的数字,句号和减号
// [^a-z] //除了小写字母以外的所有字符
// [^\\\/\^] //除了(\)(/)(^)之外的所有字符
// [^\"\'] //除了双引号(")和单引号(')之外的所有字符
// ^[-]?[0-9]+(\.[0-9]+)?$ // 所有的浮点数
//var res = /^30$|^([1-9]|[1-2][0-9])(\.\d*)?$/; 1-30的数字
//var res = /^8000$|^(1[5-9]\d)(\.\d+)?$|^([2-9]\d{2})(\.\d+)?$|^([1-7]\d{3})(\.\d+)?$/; 150-8000的数字正则表达式
//将上面的正则表达式拆解如下:
var mm1="1234 8000 150 1501 1501.2 1501.23"
console.log(mm1.match(/^8000$/)) //匹配8000
console.log(mm1.match(/^(1[5-9]\d)(\.\d+)?$/)) //匹配150-200之间的数字
console.log(mm1.match(/^([2-9]\d{2})(\.\d+)?$/)) //匹配200-1000之间的数字
console.log(mm1.match(/^([1-7]\d{3})(\.\d+)?$/)) //匹配1000-8000之间的数字
</script>
</head>
<body>
</body>
</html>