简体   繁体   中英

Make character in front mandatory / required

I need to match the following pattern: NNN

324324.234324.234324 matches, as well as 1.1.1

I have the following pattern:

(\d*\.\d*\.\d*)

Problem is, it matches NN already at the second dot, so, how can I make the following number mandatory ?

Use ^ and $ anchors and also change * to + since at least one number is required

also, (\\d*\\.){2} can be used to simplify \\d+\\.\\d+\\.

 [ '324324.234324.234324', '324324.234324.' ].forEach(test => console.log(/^(\\d+\\.){2}\\d+$/.test(test)) ); 

更改为一个或多个非贪婪+?

(\\d+?\\.\\d+?\\.\\d+)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM