正则表达式不允许连续点或下划线 [英] Regex for not allowing consecutive dots or underscores

查看:1352
本文介绍了正则表达式不允许连续点或下划线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正则表达式只允许使用字母数字字符以及点(。)和下划线(_)

I have a Regex which allows only alpha numeric characters along with dot (.) and underscore (_)

它不允许连续下划线但它允许连续点。任何人都可以指导我解决这个问题。我不想要连续的点或下划线。
以下是JavaScript函数

It does not allow consecutive underscores but it allows consecutive dots. Can anyone please guide me through this problem. I don't want consecutive dots or underscores. Below is the JavaScript function

function checkLogin() {
  var login = $("#user_login").val();
  var regex = new RegExp("^(?!.*__.*)[a-zA-Z0-9_.]+$");
  var flag = true;
  if (regex.test(login)) {
    $('#valid_character_error').css("display","none");
  } 
  else  {
    $('#valid_character_error').css("display","block");
    flag = false;
  }
  return flag;
}


推荐答案

只需包含匹配的模式负向前瞻中的两个点。

Just include the pattern which matches two dots inside the negative lookahead.

var regex = new RegExp("^(?!.*(?:__|\\.\\.))[a-zA-Z0-9_.]+$");

[a-zA-Z0-9 _。] + 将被写为 [\ w。]

更新:

var regex = new RegExp("^(?!.*?[._]{2})[a-zA-Z0-9_.]+$");

DEMO

这篇关于正则表达式不允许连续点或下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆