JS正则表达式
<!DOCTYPE html>
<html lang="">
<html lang="">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>正则表达式</title>
</head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>正则表达式</title>
</head>
<body>
<script>
var str = 'a1b2c3d4';
//用正则表达式替换数字。
//先用捕获组()捕获数字,$1(反向引用)表示第一个捕获组,$2表示第二个捕获组,以此类推。
str = str.replace(/(\d+)/g, ' <span style = "color:red"> $1 </span>');
document.write(str);
document.write("<br/>")
//面试题 把4/12/2019 用一句代码换成 2019-4-12
var a = "4/12/2019"
//证明捕获组的编号顺序从左开始一次向右数
var a = a.replace(/(((\d{1,})\/(\d{1,}))\/(\d{4}))/, '<p>1表示$1</p><p>2表示$2</p><p>3表示$3</p><p>4表示$4</p><p>5表示$5</p>')
document.write(a)
var date = "4/12/2019"
var date = date.replace(/(\d{1,})\/(\d{1,})\/(\d{4})/, '$3-$1-$2')
var a = "4/12/2019"
//证明捕获组的编号顺序从左开始一次向右数
var a = a.replace(/(((\d{1,})\/(\d{1,}))\/(\d{4}))/, '<p>1表示$1</p><p>2表示$2</p><p>3表示$3</p><p>4表示$4</p><p>5表示$5</p>')
document.write(a)
var date = "4/12/2019"
var date = date.replace(/(\d{1,})\/(\d{1,})\/(\d{4})/, '$3-$1-$2')
document.write("中文日期格式是:" + date)
//test()
//描述:检测字符串是否符合正则表达式规范
//检测"数字"是否为正正整数
var n = "a1234"
var bool = /^[1-9]\d*/.test(n)
document.write(bool) //false
</script>
</body></html>
//描述:检测字符串是否符合正则表达式规范
//检测"数字"是否为正正整数
var n = "a1234"
var bool = /^[1-9]\d*/.test(n)
document.write(bool) //false
</script>
</body></html>