猿问

这样用正则验证服务器地址跟端口号的格式是否正确格式,求教!

https://img3.mukewang.com/5c0e21670001e6aa03350316.jpg

料青山看我应如是
浏览 1003回答 1
1回答

郎朗坤

没看到你的正则,但是可以这么粗略的校验下var reg = /^[\d]{1,3}.[\d]{1,3}.[\d]{1,3}.[\d]{1,3}$/;var regp = /^[\d]+$/;var s = ['192.168.20.1.1', '192.168.20.1.1.1', '192.168.20.1a', '192.168.20.1', '192.168.20.10000'];var p = [8080, 0, 333, 'a', '1+'];for (let i = 0, len = s.length; i < len; i++) {&nbsp; console.log('ip', s[i], reg.test(s[i]))}for (let i = 0, len = p.length; i < len; i++) {&nbsp; console.log('port', p[i], regp.test(p[i]))}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答