javascript - is there a way to check if a string is of this *x.xx.xxxx* format in JavaScript?
问题描述
I want a function to check if a string is of format x.xx.xxxx in javascript. For example, s.xf.ssfs should return true. And sx.xf.hsdf should return false. Also s.fsd.sfdf should return false.
解决方案
您可以尝试使用正则表达式:
const regex = new RegExp('[a-z][.][a-z]{2}[.][a-z]{4}');
console.log(regex.test('s.xf.ssfs'));
console.log(regex.test('s.fsd.sfdf'));
作为替代方案,您还可以按句点拆分字符串并检查每个项目的长度:
function check(s){
let a = s.split('.');
return a.length == 3 && a[0].length == 1 && a[1].length == 2 && a[2].length == 4;
}
console.log(check('s.xf.ssfs'));
console.log(check('sx.xf.hsdf'));
推荐阅读
- ssl - 双层反向代理如何通过无效的 SSL 证书?
- docker - K8 的 Pod 中的 Docker 映像部署未发生
- html - 由于父标签而导致链接不起作用的子标签的问题
- c++ - 如何在没有互斥锁的情况下使共享值的行为一致?
- pandas - 在熊猫中获得三个计算的最大值
- javascript - 无法从 Javascript net::ERR_CONNECTION_REFUSED 访问 localhost
- laravel - 如何通过laravel中的交易显示银行余额
- php - 发出浏览器外请求时出现 Google 距离矩阵 URL 问题
- html - 滚动淡入
- python - 结构化流是 python + spark 3.1.1 + kafka 的唯一选择吗?