I am stuck in one RegExp where I need to validate app version for app store and play-store. I have tried several RegExp but none of them is useful for me. Here are the example that pass the test
App version up-to 2-3 decimal point
1.0 // pass
1.0.0 // pass
1.0.0.0 // fail
a.0 // fail
1 // pass
Run Code Online (Sandbox Code Playgroud)
I found one RegExp [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+ but this will only be valid when I have enter 4 decimal points. I don't know how to modify this.
Please help.
您已经提到了,up to 2-3 decimal那么RegExp必须是这个
^(\d+\.)?(\d+\.)?(\d+\.)?(\*|\d+)?$
Run Code Online (Sandbox Code Playgroud)
您可以尝试以下正则表达式
let reg = /^[0-9]((\.)[0-9]){0,2}$/
console.log(reg.test('1.0')) //true
console.log(reg.test('1.1.0')) //true
console.log(reg.test('1')) //true
console.log(reg.test('1.')) //false
console.log(reg.test('1.a')) //false
console.log(reg.test('1.1.1.1')) //falseRun Code Online (Sandbox Code Playgroud)