我想要得到所有由4位數字組成的數字。之前和之後應該有2個非數字字符或根本沒有字符。Javascript正則表達式匹配數字用非數字字符分隔
這是我到目前爲止。在這個例子中,正確的結果將只是「0000」,但它也匹配1234,4567,5678。
我錯過了什麼?
的js撥弄:http://jsfiddle.net/M8FYm/3/
來源:
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<title>Regex test</title>
<script type="text/javascript">
$(document).ready(function(){
pattern = '(\D{2})?('+'([0-9]{4})'+')(\D{2})?';
var regexp = new RegExp(pattern, "g");
var string = $('.test').html();
while (match = regexp.exec(string)) {
console.log(match);
}
})
</script>
</head>
<body>
<p class="test">
1234 4567 67
0000 345
456 23 0000
12345678
</p>
</body>
</html>