码迷,mamicode.com
首页 > Web开发 > 详细

js正则表达式

时间:2017-11-01 17:50:47      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:pattern   改变   正则表达式   检测   art   null   src   img   .exe   

语法

/pattern/attributes               /正则表达式主体/修饰符(可选)
new RegExp(pattern, attributes);

修饰符

技术分享

表达式

技术分享

元字符

技术分享

量词

技术分享

RegExp对象正则表达式方法

正则表达式.RegExp方法(字符串)

 test() 方法用于检测一个字符串是否匹配某个模式,如果字符串中含有匹配的文本,则返回 true,否则返回 false。

var patt = /^\d+$/;    //一个到多个数字并且以数字开头以数字结尾
patt.test("1203abc");      //判断这个字符串是否为[1,n]个位数字.结果为false

  var patt = /\d+/      //[1,n]位的数字
  patt.test("1203abc");  //检测中存在[1,n]数字,这里结果为true

exec() 方法检索字符串中的指定值。返回值是被找到的值。如果没有发现匹配,则返回 null。

var patt = /^\d+$/;   //[1,n]位数字的正则表达式,并且以数字开头以数字结尾
patt.exec("1203abc456");    //字符串匹配匹配正则表达式时返回,否则返回null.这里返回null

var patt = /\d+/;    //[1,n]位数字的正则表达式
patt.exec("1203abc456");    ////匹配字符串存在这个字符串

 compile() 方法也可用于改变和重新编译正则表达式。

var str="Every man in the world! Every woman on earth!";

patt=/man/g;
str2=str.replace(patt,"person");
console.info(str2);

patt=/(wo)?man/g;
patt.compile(patt);
str2=str.replace(patt,"person");
console.info(str2);

支持正则表达式的 String 对象的方法

字符串.方法(正则表达式)

技术分享

 

js正则表达式

标签:pattern   改变   正则表达式   检测   art   null   src   img   .exe   

原文地址:http://www.cnblogs.com/aeolian/p/7767385.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!