正则中开始符^和结束符$的区别

2019-05-27  本文已影响0人  郭海杰

有^时匹配必须从字符串开头开始
如:
正则 "^abc" 可以匹配"abcd" 但不能匹配"dabc"
有$时最后一个字符必须在字符串结尾

同时有^和表示必须匹配整个字符串 如: 正则"^abc"匹配字符串"abc",但不能匹配"abcd"

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>正则中开始符^和结束符$的区别</title>
</head>
<body>

<script>
var str = "Is is the cost of of gasoline going up up";
var patt1 = /^Is/;
var patt2 = /up$/;
var patt3 = /^.*$/;
document.write(str.match(patt1));
document.write("<br>");
document.write(str.match(patt2));
document.write("<br>");
document.write(str.match(patt3));
</script>
</body>
</html>
上一篇 下一篇

猜你喜欢

热点阅读