码迷,mamicode.com
首页 > 其他好文 > 详细

正则表达式RegExp详解(待续)

时间:2017-04-30 20:37:11      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:表达式   全局   regular   bsp   pre   express   对象   document   返回   

  正则表达式(Regular Expression)是一种描述字符模式的对象,RegExp 对象表示正则表达式,用来验证用户输入。

一,创建正则表达式

1,采用new运算符 

var bb=new RegExp(‘参数字符串‘,‘可选模式修饰符‘)

2,字面量法

var bb=/参数字符串/可选模式修饰符;

模式修饰符参数

i 忽略大小写

g 全局匹配

m 多行匹配

二,RegExp相关方法

test()在字符串中查找指定正则表达式,返回true或false

exec()在字符串中查找指定正则表达式,成功返回包含该查找字符串的相关信息数组,执行失败则返回null

var pattern = new RegExp(‘Box‘,‘i‘); //创建正则表达式,不区分大小写,等效于var pattern=/Box/i

var str =‘this is a box‘;
document.write(pattern.test(str));//true

document.write(pattern.exec(str));//box

 

 //使用一条语句 document.write(/Box/i.test(‘this is a box‘));

三,字符串的正则表达式方法

match(pattern)返回pattern中的子串或null

replace(pattern,replacement)

 

正则表达式RegExp详解(待续)

标签:表达式   全局   regular   bsp   pre   express   对象   document   返回   

原文地址:http://www.cnblogs.com/cumting/p/6790188.html

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