<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
<style type="text/css">
* {margin: 0; padding: 0;}
a {text-decoration: none;}
ul,li {list-style: none;}
body {font-family: "Microsoft yahei";}
</style>
</head>
<body>
<script type="text/javascript">
// match() 返回数组
var str = "123s12d34r3";
var pattern = /(\d+)/ //匹配数字一次
var pattern1= /\d+/g //匹配全部数字. g全局
console.log(str.match(pattern)[1]); //有子集 返回数组第二项是子集
console.log(str.match(pattern1));
//当正则 /阿萨/===> 需要用变量替代时
//不能用 pattern = /\d+/这种形式
// 只能 pattern = new RegExp()
// var a = "name";
// var pattern = new RegExp(a,"gi"); //全局g, 忽略大小写i. ====>第二个参数
// var str = "name=Name";
// console.log(pattern.exec(str));
// console.log(pattern.exec(str));
// \转义也需要转义 不然不识别
var pattern = new RegExp('\\d+');
var str = '123';
alert(pattern.test(str));
</script>
</body>
</html>
正则补充
最新推荐文章于 2024-06-17 00:36:41 发布