js正则表达式详解
来源:互联网 发布:装饰网络推广 编辑:程序博客网 时间:2024/06/06 03:45
js正则表达式是很多js开发人员比较头疼的事情,也很多人不愿意学习,只是必要的时候上网查一下就可以啦~本文中详细的把javascript正则表达式的用法进行了列表,希望对于大家学习javascriptt正则表达式有一定的帮助。
建立正则表达式对象语法
re = new RegExp(/pattern/[flags])
flags 参数说明:
g (全文查找出现的所有 pattern)
i (忽略大小写)
m (多行查找)
是否有匹配
- regexpObject.<SPAN>test</SPAN><SPAN>(</SPAN>string<SPAN>)</SPAN>
regexpObject.test(string)
返回值为Boolean型
- <SPAN style="FONT-WEIGHT: bold">var</SPAN> re <SPAN style="COLOR: #339933">=</SPAN> <SPAN style="FONT-WEIGHT: bold">new</SPAN> RegExp<SPAN>(</SPAN><SPAN style="FONT-STYLE: italic">/\bbe\b/g</SPAN><SPAN>)</SPAN><SPAN style="COLOR: #339933">;</SPAN>
- <SPAN style="FONT-WEIGHT: bold">var</SPAN> str <SPAN style="COLOR: #339933">=</SPAN> <SPAN style="COLOR: #3366cc">"To be, or not to be:That is the question:"</SPAN><SPAN style="COLOR: #339933">;</SPAN>
- <SPAN>alert</SPAN><SPAN>(</SPAN>str.<SPAN>search</SPAN><SPAN>(</SPAN>re<SPAN>)</SPAN><SPAN>)</SPAN><SPAN style="COLOR: #339933">;</SPAN>
var re = new RegExp(/\bbe\b/g);var str = "To be, or not to be:That is the question:";alert(str.search(re));
- string.<SPAN>search</SPAN><SPAN>(</SPAN>regexpObject<SPAN>)</SPAN>
string.search(regexpObject)
返回匹配字符的位置,无匹配返回-1
- <SPAN style="FONT-WEIGHT: bold">var</SPAN> re <SPAN style="COLOR: #339933">=</SPAN> <SPAN style="FONT-WEIGHT: bold">new</SPAN> RegExp<SPAN>(</SPAN><SPAN style="FONT-STYLE: italic">/\bbe\b/g</SPAN><SPAN>)</SPAN><SPAN style="COLOR: #339933">;</SPAN>
- <SPAN style="FONT-WEIGHT: bold">var</SPAN> str <SPAN style="COLOR: #339933">=</SPAN> <SPAN style="COLOR: #3366cc">"To be, or not to be:That is the question:"</SPAN><SPAN style="COLOR: #339933">;</SPAN>
- <SPAN>alert</SPAN><SPAN>(</SPAN>re.<SPAN>test</SPAN><SPAN>(</SPAN>str<SPAN>)</SPAN><SPAN>)</SPAN><SPAN style="COLOR: #339933">;</SPAN>
var re = new RegExp(/\bbe\b/g);var str = "To be, or not to be:That is the question:";alert(re.test(str));
取得正则匹配信息
- regexpObject.<SPAN>exec</SPAN><SPAN>(</SPAN>string<SPAN>)</SPAN>
regexpObject.exec(string)
- <SPAN style="FONT-WEIGHT: bold">var</SPAN> re <SPAN style="COLOR: #339933">=</SPAN> <SPAN style="FONT-WEIGHT: bold">new</SPAN> RegExp<SPAN>(</SPAN><SPAN style="FONT-STYLE: italic">/be/g</SPAN><SPAN>)</SPAN><SPAN style="COLOR: #339933">;</SPAN>
- <SPAN style="FONT-WEIGHT: bold">var</SPAN> str <SPAN style="COLOR: #339933">=</SPAN> <SPAN style="COLOR: #3366cc">"To be, or not to be:That is the question:"</SPAN><SPAN style="COLOR: #339933">;</SPAN>
- <SPAN style="FONT-WEIGHT: bold">var</SPAN> f<SPAN style="COLOR: #339933">;</SPAN>
- <SPAN style="FONT-WEIGHT: bold">do</SPAN>
- <SPAN>{</SPAN>
- f <SPAN style="COLOR: #339933">=</SPAN> re.<SPAN>exec</SPAN><SPAN>(</SPAN>str<SPAN>)</SPAN><SPAN style="COLOR: #339933">;</SPAN>
- <SPAN>alert</SPAN><SPAN>(</SPAN>f <SPAN style="COLOR: #339933">+</SPAN> <SPAN style="COLOR: #3366cc">":"</SPAN> <SPAN style="COLOR: #339933">+</SPAN> f.<SPAN>index</SPAN><SPAN>)</SPAN><SPAN style="COLOR: #339933">;</SPAN>
- <SPAN>}</SPAN> <SPAN style="FONT-WEIGHT: bold">while</SPAN> <SPAN>(</SPAN>f<SPAN style="COLOR: #339933">!=</SPAN><SPAN style="FONT-WEIGHT: bold">null</SPAN><SPAN>)</SPAN><SPAN style="COLOR: #339933">;</SPAN>
var re = new RegExp(/be/g);var str = "To be, or not to be:That is the question:";var f;do{ f = re.exec(str); alert(f + ":" + f.index);} while (f!=null);
使用正则表达式进行字符串替换
- string.<SPAN>replace</SPAN><SPAN>(</SPAN>re<SPAN style="COLOR: #339933">,</SPAN> replaceString<SPAN>)</SPAN>
string.replace(re, replaceString)
- <SPAN style="FONT-WEIGHT: bold">var</SPAN> re <SPAN style="COLOR: #339933">=</SPAN> <SPAN style="FONT-WEIGHT: bold">new</SPAN> RegExp<SPAN>(</SPAN><SPAN style="FONT-STYLE: italic">/be/g</SPAN><SPAN>)</SPAN><SPAN style="COLOR: #339933">;</SPAN>
- <SPAN style="FONT-WEIGHT: bold">var</SPAN> str <SPAN style="COLOR: #339933">=</SPAN> <SPAN style="COLOR: #3366cc">"To be, or not to be:That is the question:"</SPAN><SPAN style="COLOR: #339933">;</SPAN>
- <SPAN>alert</SPAN><SPAN>(</SPAN>str.<SPAN>replace</SPAN><SPAN>(</SPAN>re<SPAN style="COLOR: #339933">,</SPAN> <SPAN style="COLOR: #3366cc">"*"</SPAN><SPAN>)</SPAN><SPAN>)</SPAN><SPAN style="COLOR: #339933">;</SPAN>
var re = new RegExp(/be/g);var str = "To be, or not to be:That is the question:";alert(str.replace(re, "*"));
- JS正则表达式详解
- JS正则表达式详解
- JS正则表达式详解
- JS正则表达式详解
- JS正则表达式详解
- JS正则表达式详解
- JS正则表达式详解
- JS正则表达式详解
- JS正则表达式详解
- JS正则表达式详解
- JS正则表达式详解
- JS正则表达式详解
- JS正则表达式详解
- js正则表达式详解
- JS正则表达式详解
- js正则表达式详解
- JS正则表达式详解
- js正则表达式详解
- 递归深度优先遍历图(DFS)(邻接表和数组两种方式)
- Oracle使用SQL脚本创建表空间,用户,分配权限
- 传说中的ACM大牛!!!
- oracle创建表空间
- listView上拉刷新下拉加载
- js正则表达式详解
- 【无限互联】iOS开发视频教程— 2.6 iPhone开发之if条件和for循环语句
- HTML 学习连接
- Mono for Android 安装配置方法 附破解版
- rhel 安装5.5yum包 并安装mplayer (第二版)
- 用户自动登录的jsp实现
- TreeView节点 (前台JS 设置 Postback 触发TreeNodeCheckChanged 事件)
- 如果不是异地
- 生成release版本的Android系统