![](/img/trans.png)
[英]How can I use a variable with RegEx in JavaScript for an exact match using “^” and “$”?
[英]How to match exact digits count by using regex in javascript
我需要在所有點 (。)之后找到3位數字。 如果輸入“ 1.22.222” ,則應返回false 。 如果我給出“ 1.222.222”,那么它應該返回true 。 我已經嘗試了下面的正則表達式,但是沒有用。
var reg1 = new RegExp("\\.\\d{3}");
var reg2 = new RegExp("\\.\\d{3,3}");
reg1.test("1.22.222") // returns true, but i need to return false.
如何解決這個問題。
使用此^\\d(\\.\\d{3})+$
也許您可以使用^[^\\.]+(?:\\.\\d{3})+$
這將匹配
^ # From the beginning of the string [^\.]+ # match NOT a dot one or more times (?: # A non capturing group \.\d{3} # Match a dot and 3 digits ) # Close non capturing group and repeat one or more times $ # The end of the string
var reg = new RegExp(“ ^ \\ d(\\。\\ d {3})”)
var out = reg.test(“ 1.222.222”)console.log(out)// true
var out = reg.test(“ 1.22.222”)console.log(out)//否
var out = reg.test(“ 1.222.22”)console.log(out)//否
它需要以點之前的東西開頭,並以點之前的3位數字結尾
var reg = new RegExp("^\\d(\\.\\d{3})+$");
var out = reg.test("1.222.222")
console.log(out) // true
var out = reg.test("1.22.222")
console.log(out) // false
var out = reg.test("1.222.22")
console.log(out) // false
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.