JavaScript 中使用正则表达式

创建正则对象

方式1:

  1. var reg = new Regex('\d', 'i');
  2. var reg = new Regex('\d', 'gi');

方式2:

  1. var reg = /\d/i;
  2. var reg = /\d/gi;

参数

标志 说明
i 忽略大小写
g 全局匹配
gi 全局匹配+忽略大小写

正则匹配

  1. // 匹配日期
  2. var dateStr = '2015-10-10';
  3. var reg = /^\d{4}-\d{1,2}-\d{1,2}$/
  4. console.log(reg.test(dateStr));

正则提取

  1. // 1. 提取工资
  2. var str = "张三:1000,李四:5000,王五:8000。";
  3. var array = str.match(/\d+/g);
  4. console.log(array);
  5. // 2. 提取email地址
  6. var str = "123123@xx.com,fangfang@valuedopinions.cn 286669312@qq.com 2、emailenglish@emailenglish.englishtown.com 286669312@qq.com...";
  7. var array = str.match(/\w+@\w+\.\w+(\.\w+)?/g);
  8. console.log(array);
  9. // 3. 分组提取
  10. // 3. 提取日期中的年部分 2015-5-10
  11. var dateStr = '2016-1-5';
  12. // 正则表达式中的()作为分组来使用,获取分组匹配到的结果用Regex.$1 $2 $3....来获取
  13. var reg = /(\d{4})-\d{1,2}-\d{1,2}/;
  14. if (reg.test(dateStr)) {
  15. console.log(RegExp.$1);
  16. }
  17. // 4. 提取邮件中的每一部分
  18. var reg = /(\w+)@(\w+)\.(\w+)(\.\w+)?/;
  19. var str = "123123@xx.com";
  20. if (reg.test(str)) {
  21. console.log(RegExp.$1);
  22. console.log(RegExp.$2);
  23. console.log(RegExp.$3);
  24. }

正则替换

  1. // 1. 替换所有空白
  2. var str = " 123AD asadf asadfasf adf ";
  3. str = str.replace(/\s/g,"xx");
  4. console.log(str);
  5. // 2. 替换所有,|,
  6. var str = "abc,efg,123,abc,123,a";
  7. str = str.replace(/,|,/g, ".");
  8. console.log(str);

案例:表单验证

  1. QQ号:<input type="text" id="txtQQ"><span></span><br>
  2. 邮箱:<input type="text" id="txtEMail"><span></span><br>
  3. 手机:<input type="text" id="txtPhone"><span></span><br>
  4. 生日:<input type="text" id="txtBirthday"><span></span><br>
  5. 姓名:<input type="text" id="txtName"><span></span><br>
  1. //获取文本框
  2. var txtQQ = document.getElementById("txtQQ");
  3. var txtEMail = document.getElementById("txtEMail");
  4. var txtPhone = document.getElementById("txtPhone");
  5. var txtBirthday = document.getElementById("txtBirthday");
  6. var txtName = document.getElementById("txtName");
  7. //
  8. txtQQ.onblur = function () {
  9. //获取当前文本框对应的span
  10. var span = this.nextElementSibling;
  11. var reg = /^\d{5,12}$/;
  12. //判断验证是否成功
  13. if(!reg.test(this.value) ){
  14. //验证不成功
  15. span.innerText = "请输入正确的QQ号";
  16. span.style.color = "red";
  17. }else{
  18. //验证成功
  19. span.innerText = "";
  20. span.style.color = "";
  21. }
  22. };
  23. //txtEMail
  24. txtEMail.onblur = function () {
  25. //获取当前文本框对应的span
  26. var span = this.nextElementSibling;
  27. var reg = /^\w+@\w+\.\w+(\.\w+)?$/;
  28. //判断验证是否成功
  29. if(!reg.test(this.value) ){
  30. //验证不成功
  31. span.innerText = "请输入正确的EMail地址";
  32. span.style.color = "red";
  33. }else{
  34. //验证成功
  35. span.innerText = "";
  36. span.style.color = "";
  37. }
  38. };

表单验证部分,封装成函数:

  1. var regBirthday = /^\d{4}-\d{1,2}-\d{1,2}$/;
  2. addCheck(txtBirthday, regBirthday, "请输入正确的出生日期");
  3. //给文本框添加验证
  4. function addCheck(element, reg, tip) {
  5. element.onblur = function () {
  6. //获取当前文本框对应的span
  7. var span = this.nextElementSibling;
  8. //判断验证是否成功
  9. if(!reg.test(this.value) ){
  10. //验证不成功
  11. span.innerText = tip;
  12. span.style.color = "red";
  13. }else{
  14. //验证成功
  15. span.innerText = "";
  16. span.style.color = "";
  17. }
  18. };
  19. }

通过给元素增加自定义验证属性对表单进行验证:

  1. <form id="frm">
  2. QQ号:<input type="text" name="txtQQ" data-rule="qq"><span></span><br>
  3. 邮箱:<input type="text" name="txtEMail" data-rule="email"><span></span><br>
  4. 手机:<input type="text" name="txtPhone" data-rule="phone"><span></span><br>
  5. 生日:<input type="text" name="txtBirthday" data-rule="date"><span></span><br>
  6. 姓名:<input type="text" name="txtName" data-rule="cn"><span></span><br>
  7. </form>
  1. // 所有的验证规则
  2. var rules = [
  3. {
  4. name: 'qq',
  5. reg: /^\d{5,12}$/,
  6. tip: "请输入正确的QQ"
  7. },
  8. {
  9. name: 'email',
  10. reg: /^\w+@\w+\.\w+(\.\w+)?$/,
  11. tip: "请输入正确的邮箱地址"
  12. },
  13. {
  14. name: 'phone',
  15. reg: /^\d{11}$/,
  16. tip: "请输入正确的手机号码"
  17. },
  18. {
  19. name: 'date',
  20. reg: /^\d{4}-\d{1,2}-\d{1,2}$/,
  21. tip: "请输入正确的出生日期"
  22. },
  23. {
  24. name: 'cn',
  25. reg: /^[\u4e00-\u9fa5]{2,4}$/,
  26. tip: "请输入正确的姓名"
  27. }];
  28. addCheck('frm');
  29. //给文本框添加验证
  30. function addCheck(formId) {
  31. var i = 0,
  32. len = 0,
  33. frm =document.getElementById(formId);
  34. len = frm.children.length;
  35. for (; i < len; i++) {
  36. var element = frm.children[i];
  37. // 表单元素中有name属性的元素添加验证
  38. if (element.name) {
  39. element.onblur = function () {
  40. // 使用dataset获取data-自定义属性的值
  41. var ruleName = this.dataset.rule;
  42. var rule =getRuleByRuleName(rules, ruleName);
  43. var span = this.nextElementSibling;
  44. //判断验证是否成功
  45. if(!rule.reg.test(this.value) ){
  46. //验证不成功
  47. span.innerText = rule.tip;
  48. span.style.color = "red";
  49. }else{
  50. //验证成功
  51. span.innerText = "";
  52. span.style.color = "";
  53. }
  54. }
  55. }
  56. }
  57. }
  58. // 根据规则的名称获取规则对象
  59. function getRuleByRuleName(rules, ruleName) {
  60. var i = 0,
  61. len = rules.length;
  62. var rule = null;
  63. for (; i < len; i++) {
  64. if (rules[i].name == ruleName) {
  65. rule = rules[i];
  66. break;
  67. }
  68. }
  69. return rule;
  70. }