下午做完事情,看了一下match的用法
我自己的理解是:如果match方法不加入g,就返回第一个匹配,如果加入了g,则返回所匹配的数组的所有内容(是一个数组)。
下面我来试验一下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>match</title>
</head>
<body>
<script>
var str = "abcd2004-08testok2009-09op";
var ms = str.match(/\d{4}\-\d{2}/);
document.write(ms);
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>match</title>
</head>
<body>
<script>
var str = "abcd2004-08testok2009-09op";
var ms = str.match(/\d{4}\-\d{2}/);
document.write(ms);
</script>
</body>
</html>
结果:2004-08
当加入了g,页面代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>match</title>
</head>
<body>
<script>
var str = "abcd2004-08testok2009-09op";
var ms = str.match(/\d{4}\-\d{2}/g);
document.write(ms);
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>match</title>
</head>
<body>
<script>
var str = "abcd2004-08testok2009-09op";
var ms = str.match(/\d{4}\-\d{2}/g);
document.write(ms);
</script>
</body>
</html>
结果:2004-08,2009-09
2.如果我希望提取的2004-08中的月份08,应该如何呢?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>match</title>
</head>
<body>
<script>
var str = "abcd2004-08testok2009-09op";
var ms = str.match(/\d{4}\-\d{2}/g);
var ms2 = ms[0].match(/\d{2}$/);
document.write(ms2);
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>match</title>
</head>
<body>
<script>
var str = "abcd2004-08testok2009-09op";
var ms = str.match(/\d{4}\-\d{2}/g);
var ms2 = ms[0].match(/\d{2}$/);
document.write(ms2);
</script>
</body>
</html>
这是我自己愚钝的方法,写了2个正则表达式
后来经过指点,学到了更好的正则表达式
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>match</title>
</head>
<body>
<script>
var str = "abcd2004-08testok2009-09op";
var ms = str.match(/(\d{4})\-(\d{2})/);
document.write(ms);
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>match</title>
</head>
<body>
<script>
var str = "abcd2004-08testok2009-09op";
var ms = str.match(/(\d{4})\-(\d{2})/);
document.write(ms);
</script>
</body>
</html>
结果:2004-08,2004,08
对年和月单独用了括号,这样得到的数组就是第一个匹配的,第一个括号里匹配的,第二个括号里匹配的(注意此时没有g)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>match</title>
</head>
<body>
<script>
var str = "abcd2004-08testok2009-09op";
var ms = str.match(/(\d{4})\-(\d{2})/);
document.write(ms[2]);
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>match</title>
</head>
<body>
<script>
var str = "abcd2004-08testok2009-09op";
var ms = str.match(/(\d{4})\-(\d{2})/);
document.write(ms[2]);
</script>
</body>
</html>
结果:08
这时候会产生一个全局变量RegExp,我们还可以这样
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>match</title>
</head>
<body>
<script>
var str = "abcd2004-08testok2009-09op";
var ms = str.match(/(\d{4})\-(\d{2})/);
//document.write(ms[2]);
document.write(RegExp.$2);
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>match</title>
</head>
<body>
<script>
var str = "abcd2004-08testok2009-09op";
var ms = str.match(/(\d{4})\-(\d{2})/);
//document.write(ms[2]);
document.write(RegExp.$2);
</script>
</body>
</html>
今天的收获比较大,搞清楚的match的用法!~~
并且把含糊不清的^,$也搞清白了,^是匹配字符串的开始,而不是正则的开始,之前理解的有问题