网址匹配的正则

本文探讨了如何使用正则表达式进行网址验证,特别是在JavaScript和Vue.js环境中,通过监听input值变化实现输入框的有效性检查。

下面展示一些 网址匹配的正则

// 直接匹配域名地址:
	
var matchString = 'https://www.bilibili.com/';
console.log(match.test(matchString)); // ==> true


// 匹配链接含(*.htm,*.html,*.php,*.aspx...)后缀的地址:
	
var matchString = 'https://i.cnblogs.com/EditPosts.aspx';
console.log(match.test(matchString)); // ==> true


// 匹配含参数的地址:
	
var matchString = 'https://i.cnblogs.com/EditPosts.aspx?opt=1';
console.log(match.test(matchString)); // ==> true

输入框需要验证正则的话,绑定一个监听input值变化的方法就可以了

//这里展示ele ui 里的
	
 <el-input v-model="form.goodUrl" size="mini" style="width:200px" placeholder="输入宝贝链接" @input="checkUrl"></el-input>


//script里这样写

  checkUrl(e) {
      console.log(e);
      var match = /^((ht|f)tps?):\/\/[\w\-]+(\.[\w\-]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?$/;
      var matchString = e;
      console.log(match.test(matchString)); //这里输出布尔值
      if (match.test(matchString)) {
        //验证成功
      } else {
        //验证失败
      }
    },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值