一、RegExp

1.1 创建RegExp对象

new RegExp("必选,正则表达式","可选,匹配模式g,i,m")

1.2 RegExp对象的方法

  • test:检索字符串中的指定值,返回True或False。

  • exec:检索字符串中的指定值,返回找到的值,没有则null。

  • complie:用于改变正则表达式,或增删匹配模式。

1.2.1 test()
var r1 = new RegExp('world');
console.log(r1.test('Hello, world!')); //true
console.log(r1.test('Hello, World!')); //false
var r2 = new RegExp('world', 'i'); //大小写不敏感
console.log(r2.test('Hello, World!')); //true
var r3 = new RegExp(/world/i); //简写
console.log(r3.test('Hello, World!')); //true
1.2.2 exec()
var r1 = new RegExp('world');
console.log(r1.exec('Hello, world!')); //['world']
console.log(r1.exec('Hello, World!')); //null
var r2 = new RegExp('world', 'i'); //大小写不敏感
console.log(r2.exec('Hello, World!')); //['world']
var r3 = new RegExp(/world/i); //简写
console.log(r3.exec('Hello, World!')); //['world']
var r4 = new RegExp('o');
console.log(r4.exec('Hello, World!')); //['o']
var r5 = new RegExp('o', 'gi');
console.log(r5.exec('Hello, WOrld!')); //['o']
console.log(r5.lastIndex); //5 匹配文本的第一个字符的位置,o为4,下一个位置为5
console.log(r5.exec('Hello, WOrld!')); //['O'] 匹配完第一个o后调用继续匹配
console.log(r5.lastIndex); //9
console.log(r5.exec('Hello, WOrld!')); //null 匹配不到返回null
console.log(r5.lastIndex); //0 lastIndex重置为0
1.2.3 complie()
var r1 = new RegExp('world');
console.log(r1.exec('Hello, world!')); //['world']
r1.compile('o');
console.log(r1.exec('Hello, World!')); //['o']
r1.compile('m');
console.log(r1.exec('Hello, World!')); //null
var r2 = new RegExp('world');
console.log(r2.test('Hello, world!')); //true
r2.compile('mazey');
console.log(r2.test('Hello, world!')); //false

二、正则表达式

  • ^$:表示匹配值的开始和结尾。

  • +:1+,一个或更多。

  • *:0+,零个或更多。

  • ?:0/1,零个或一个。

  • {1,2}:1

    <=length<=2,长度。< li="">

  • ():表示一个表达式的组。

  • []:匹配的字符范围,我理解为一个块,很多块放在一个组()里面。

三、示例

<form action="">
输入:<input type="text" name="mazey" id="mazey" placeholder="请输入邮箱">
<input type="button" value="验证" οnclick="check();">
</form>
<script>
function check(){var reg = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$"); //正则表达式var obj = document.getElementById("mazey"); //要验证的对象if(obj.value === ""){ //输入不能为空alert("输入不能为空!");return false;}else if(!reg.test(obj.value)){ //正则验证不通过,格式不对alert("验证不通过!");return false;}else{alert("通过!");return true;}
}
</script>

JavaScript邮箱验证-正则验证

转载于:https://blog.51cto.com/mazey/1948712

JavaScript邮箱验证-正则验证相关推荐

  1. JS邮箱验证_手机号码验证_电话号码验证-正则验证

    一.正则表达式 //对电子邮件的验证:^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$ //对 ...

  2. php 验证手机号邮箱,PHP正则验证真实姓名、手机号码、邮箱

    在开发中,通常会遇见简单的表单验证,希望快速获取用户提交信息,但是为了安全考虑,我们不光前端需要做js验证,后端也需要做相应的验证,确保不是恶意提交的信息,以下是php通过正则来验证真实姓名.手机号码 ...

  3. thinkphp3.2.3 自动验证 正则验证

    <?phpnamespace Home1\Model;use Think\Model;class ShopYuyueInfoModel extends Model {// protected $ ...

  4. ASP用正则验证邮箱地址手机号码电话号码格式

    经常需要验证邮箱地址,手机号码,电话号码等.在这儿把ASP中的正则验证实现记录一下 Function validate(ByVal str,ByVal number) Dim temp,reg Set ...

  5. html 邮政编码格式,js与jquery正则验证电子邮箱、手机号、邮政编码的方法

    本文实例讲述了js与jquery正则验证电子邮箱.手机号.邮政编码的方法. jQuery代码: //验证邮政编码 $("#postcode").blur(function(){ / ...

  6. 在登录页面中js进行正则验证电话号码和邮箱地址,并使用ajax进行用户ID的数据库验证

    本文说的是在登录页面中使用js进行正则验证并使用ajax进行用户ID的数据库验证,另外也加入键盘监听. 先看一下登录的jsp页面代码 <%@ page language="java&q ...

  7. html验证邮箱和手机号,js与jquery正则验证电子邮箱、手机号、邮政编码的方法

    本文实例讲述了js与jquery正则验证电子邮箱.手机号.邮政编码的方法. jQuery代码: 获取邮政编码 var postcode=$("#postcode").val(); ...

  8. php获取邮箱内容吗,php正则验证email邮箱及抽取内容中email的例子

    1,php正则验证email格式: 复制代码 代码示例: if (ereg("/^[a-z]([a-z0-9]*[-_\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0 ...

  9. jquery中邮箱地址 URL网站地址正则验证实例代码

    jquery中邮箱地址 URL网站地址正则验证实例代码 QQ网站有一个网站举报的功能,看了一些js代码觉得写得很不错,我就拿下来了,下面是一个email验证与url网址验证js代码,分享给大家 ema ...

  10. 常用正则验证(邮箱、手机号、密码)

    1.邮箱正则验证 var reg = /^([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+[\\.][A-Za-z]{ ...

最新文章

  1. Windows 8 开发31日-第04日-新控件
  2. linux下搭建nagios监控
  3. PHP和tp5—使用过得函数总结
  4. LeetCode每日一题——两数相加
  5. cs231n学习笔记-激活函数-BN-参数优化
  6. 常用css样式大全以及css属性代码大全
  7. java极光推送demo_Java集成极光推送
  8. axure:原型简单使用
  9. Java实战---搜搜移动业务大厅
  10. Apache Log4j2远程代码执行漏洞复现
  11. android qq音乐 搜索,QQ音乐搜索功能基本思路
  12. 培训班出身的程序员为什么遭人嫌弃
  13. 通达信日线数据用转换为excel、csv和feather格式
  14. c语言程序设计 大学考试题库,网络教育成考大学C语言程序设计考试题库及答案...
  15. 微信公众号接入微软小冰
  16. spacy POS 和 Syntactic Dependency Parsing 的标记
  17. easyUI 正则表达式验证
  18. codewars解题笔记---Are You Playing Banjo?
  19. AB32实例应用(4.非常规经验及技巧)
  20. helm create configmap error: ConfigMap in version “v1“ cannot be handled as a ConfigMap

热门文章

  1. http和https的区别,不懂的一起研究【转】
  2. Hi3520d 网卡驱动源码分析
  3. html正常php不正常,不完全的HTML头消息,可能被某些PHP服务器拒绝.
  4. 使用pjsip传输已经编码的视频,源码在github
  5. python程序中name的作用_python编程中的if __name__ == '__main__': 的作用和原理
  6. 介绍KMP算法思想(例题:ACWING 831 kmp字符串)
  7. 深度学习优化算法大全系列1:概览
  8. SLAM学习笔记-------------(12)建图
  9. ASP.NET---操作数据库增删改查
  10. git 公钥提交代码_Git自由之章 - 关于SSH 公钥