浅谈Javascript常用正则表达式应用

所属分类: 网络编程 / JavaScript 阅读数: 1640
收藏 0 赞 0 分享

模式修饰符的可选参数

  • i: 忽略大小写
  • g: 全局匹配
  • m: 多行匹配
  • /hello/: 两个反斜杠是正则表达式的字面量表示法

两个测试方法

test

const test = new RegExp('hello world', 'ig');
console.log(test.test('hello world')); // true

exec

返回的是数组,有就返回数组的值,没有返回为null

const test = new RegExp('hello world', 'ig');
console.log(test.exec('hello')); // null

四个正则表达式方法

match(pattern)

将所有匹配的字符串组合成数组返回

const pattern=/Box/ig;
const str="This is a Box! The is a box!";
console.log(str.match(pattern));

search(pattern)

返回字符串中pattern开始位置,忽略全局匹配

const pattern=/Box/i;	//
const str="This is a Box! The is a box!";
console.log(str.search(pattern)); // 10

replace(pattern)

替换匹配到的字符串

const pattern=/Box/ig;
const str="This is a Box! The is a box!";
console.log(str.replace(pattern,'Tom'));

split(pattern)

返回字符串指定pattern拆分数组

const pattern = / /ig;	//空格
const str = "This is a Box! The is a box!";
console.log(str.split(pattern)); //以空格进行分割,返回的是数组
// 输出结果
// [ 'This', 'is', 'a', 'Box!', 'The', 'is', 'a', 'box!' ]

匹配模式

\w表示a-zA-Z_
锚元字符匹配(^ $) ^强制收匹配 $强制尾匹配,并且只匹配一个

const pattern=/^[a-z]oogle\d$/;
const str="aoogle2";
console.log(pattern.test(str)); // true

注意: ^符号在[]里面表示 非 在外边表示强制首匹配,并且只匹配一个 要想匹配多个值,使用+
\b表示到达边界

|表示匹配或选择模式

const pattern=/baidu|google|bing/; //匹配或选择其中某个字符,不是相等,包含的意思
const str = "baidu a google"; 
console.log(pattern.test(str)); //返回true

常用正则表达式

检查邮政编码

const pattern = /^[1-9]{1}[0-9]{5}$/;
const str = "122534"; //共6位数,第一位不能为0
console.log(pattern.test(str)); // true

压缩包后缀名
\w等于a-zA-Z0-9_ 使用^限定从首字母匹配 .是特殊符号需要\n进行转义
|选择符必须使用()进行分组

const pattern = /^[\w]+\.(zip|gz|rar)$/; 
const str="a12_.zip"; //文件名 字母_数字.zip,gz,rar
console.log(pattern.test(str)); // true

删除多余空格

方法一: 使用replace只匹配一个,所以使用+匹配多个

 var pattern=/^\s+/; 
 var str=" google ";
 var result=str.replace(pattern,'');
 pattern=/\s+$/;
 result=result.replace(pattern,'');
 

方法二: (.+)贪婪模式,使用惰性模式,后面的空格不让匹配

var pattern=/^\s+(.+?)\s+$/;
 var str=" google ";
 var result=pattern.exec(str,'')[1];

 console.log('|'+result+'|');

方法三: (.+)贪婪模式,改为惰性模式,使用分组模式,只取匹配的内容

var pattern=/^\s+(.+?)\s+$/;
 var str=" google ";
 var result=str.replace(pattern,'$1'); //使用分组模式

 console.log('|'+result+'|'); // |google|

简单邮箱验证

var pattern=/^([\w\.\_]+)@([\w\_]+)\.([a-zA-Z]){2,4}$/;
var str="qzfweb@gmail.com";
console.log(pattern.test(str)); // true

以上所述是小编给大家介绍的Javascript常用正则表达式应用讲解详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

jQuery LigerUI 使用教程表格篇(1)

ligerGrid是ligerui系列插件的核心控件,用户可以快速地创建一个美观,而且功能强大的表格,支持排序、分页、多表头、固定列等等
收藏 0 赞 0 分享

JavaScript中常用的运算符小结

JavaScript中常用的运算符小结,需要的朋友可以参考下。
收藏 0 赞 0 分享

深入理解JavaScript系列(13) This? Yes,this!

在这篇文章里,我们将讨论跟执行上下文直接相关的更多细节。讨论的主题就是this关键字。实践证明,这个主题很难,在不同执行上下文中this的确定经常会发生问题
收藏 0 赞 0 分享

javascript (用setTimeout而非setInterval)

javascript (用setTimeout而非setInterval)如果用setInterval 可能出现 下次调用会在前一次调用前调用
收藏 0 赞 0 分享

JavaScript中两个感叹号的作用说明

用两个感叹号的作用就在于,如果明确设置了o中flag的值(非null/undefined/0""/等值),自然test就会取跟o.flag一样的值;如果没有设置,test就会默认为false,而不是null或undefined
收藏 0 赞 0 分享

javascript写的简单的计算器,内容很多,方法实用,推荐

最近用javascript写了一个简单的计算器,自己测试感觉还好,代码都给了注释,非常不错,推荐大家学习。
收藏 0 赞 0 分享

js的表单操作 简单计算器

javascript写的简单的加减乘除计算器,里面涉及到一些方法还是很实用的哦,新手不要错过
收藏 0 赞 0 分享

Jquery中删除元素的实现代码

empty用来删除指定元素的子元素,remove用来删除元素,或者设定细化条件执行删除
收藏 0 赞 0 分享

javaScript 利用闭包模拟对象的私有属性

JavaScript缺少块级作用域,没有private修饰符,但它具有函数作用域。作用域的好处是内部函数可以访问它们的外部函数的参数和变量(除了this和argument
收藏 0 赞 0 分享

为JavaScript类型增加方法的实现代码(增加功能)

大家在js开发过程中有些功能已经满足不了我们的需求,或没有我们需要的功能,那么我们就可以自己扩展下,个性化js
收藏 0 赞 0 分享
查看更多