<div class="iteye-blog-content-contain" style="font-size: 14px"></div>
定义和使用
var patt1 = newRegExp("hello");
var patt2 = /world/ ;
test方法
test() 方法检索字符串中的指定值。返回值是 true 或 false。
var pat = /my/;
var str = "this is my code...";
console.log(pat.test(str)); // true
exec方法
exec() 方法检索字符串中的指定值。返回值是被找到的值。如果没有发现匹配,则返回 null。
var pat = /hello/;
console.log(pat.exec("oh hello world")); //返还hello
正则表达式类型
/pattern/attributes
参数 attributes 是一个可选的字符串,常用属性 "g"、"i" ,分别用于指定全局匹配、区分大小写的匹配。
//不区分大小写
var str = "Visit Hunger";
var patt1 = /hunger/i;
console.log(str.match(patt1)); //全局匹配
var str="hello hunger valley! I am hunger"; var patt1=/hunger/g; console.log(str.match(patt1)); //不区分大小写,全局匹配var str="hello Hunger valley! I am hunger"; var patt1=/hunger/gi; console.log(str.match(patt1));
字符串正则
1. search
字符串查找
var str="Visit W3School!";
console.log(str.search(/w3school/)); //-1console.log(str.serach(/w3school/i)); // 6
2. match
字符串匹配
var str="1 plus 2 equal 33";
console.log(str.match(/\d+/)); //[1]
console.log(str.match(/\d+/g)); //[1,2,33]
3. replace
字符串替换
var str="Hello JI! oh I am hunger"
console.log(str.replace(/Hunger/, "valley")); console.log(str.replace(/hunger/ig, "hunger"));
4. split
字符串分割
var str = "Hello Hunger , oh I am Hunger";
str.split("");
str.split(/\s+/);
正则写法
<!--[if !supportLists]-->· <!--[endif]-->[abc] 查找方括号之间的任何字符。
var str="Is this all there is?";
var patt1=/[a-h]/g;
console.log(str.match(patt1));
<!--[if !supportLists]-->· <!--[endif]-->[^abc] 查找任何不在方括号之间的字符。
var str="hello jikexueyuan!";
var patt1=/[^jike]/g;
console.log(str.match(patt1));
<!--[if !supportLists]-->· <!--[endif]-->[0-9] 查找任何从 0 至 9 的数字。
<!--[if !supportLists]-->· <!--[endif]-->[a-z] 查找任何从小写 a 到小写 z 的字符。
<!--[if !supportLists]-->· <!--[endif]-->[A-Z] 查找任何从大写 A 到大写 Z 的字符。
<!--[if !supportLists]-->· <!--[endif]-->[A-z] 查找任何从大写 A 到小写 z 的字符。
<!--[if !supportLists]-->· <!--[endif]-->[adgk] 查找给定集合内的任何字符。
<!--[if !supportLists]-->· <!--[endif]-->[^adgk] 查找给定集合外的任何字符。
<!--[if !supportLists]-->· <!--[endif]-->red|blue|green 查找任何指定的选项。
var str="hello hunger! How are you?";
var patt1=/hello|you/g; c
onsole.log(str.match(patt1));
<!--[if !supportLists]-->· <!--[endif]-->. 查找单个字符,除了换行和行结束符。
var str="That's hot!";
var patt1=/h.t/g;
document.write(str.match(patt1));
<!--[if !supportLists]-->· <!--[endif]-->\w 查找单词字符(字母、数字、下划线)。
var str="Give 100%!";
var patt1=/\w/g;
document.write(str.match(patt1));
<!--[if !supportLists]-->· <!--[endif]-->\W 查找非单词字符。
var str="Give 100%!"; var patt1=/\W/g; document.write(str.match(patt1));
<!--[if !supportLists]-->· <!--[endif]-->\d 查找数字。
var str="Give 100%!";
var patt1=/\d/g;
document.write(str.match(patt1));
<!--[if !supportLists]-->· <!--[endif]-->\D 查找非数字字符。
var str="Give 100%!"; var patt1=/\D/g; document.write(str.match(patt1));
<!--[if !supportLists]-->· <!--[endif]-->\s 查找空白字符(空格、tab、换行、回车)。
var str="Is this all there is?";
var patt1=/\s/g; document.write(str.match(patt1));
<!--[if !supportLists]-->· <!--[endif]-->\S 查找非空白字符。
var str="Is this all there is?"; var patt1=/\S/g; document.write(str.match(patt1));
<!--[if !supportLists]-->· <!--[endif]-->\b 匹配单词边界。
/\bm/ 匹配"moon"中的'm';
/oo\b/ 不匹配"moon"中的'oo',因为'oo'后面的'n'是一个单词字符;
/oon\b/ 匹配"moon"中的'oon',因为'oon'位于字符串的末端,后面没有单词字符; var str="Hello jikexueyuan";
var patt1=/\bjikexueyuan/g;
document.write(str.match(patt1));
<!--[if !supportLists]-->· <!--[endif]-->\B 匹配非单词边界。
<!--[if !supportLists]-->· <!--[endif]-->\n 查找换行符。
var str="Hello Hunger.\n be a FE.";
var patt1=/\n/g; document.write(str.search(patt1));
<!--[if !supportLists]-->· <!--[endif]-->n+ 匹配任何包含至少一个 n 的字符串。
var str="Hello HHunger! Hello World!";
var patt1=/H+/g;
document.write(str.match(patt1));
var str="Hello Hunger! Hello World!";
var patt1=/\w+/g; document.write(str.match(patt1));
<!--[if !supportLists]-->· <!--[endif]-->n* 匹配任何包含零个或多个 n 的字符串。
var str="Hellooo Hunger! Hello World!"; var patt1=/lo*/g; document.write(str.match(patt1))
<!--[if !supportLists]-->· <!--[endif]-->n? 匹配任何包含零个或一个 n 的字符串。
var str="1, 100 or 1000?"; var patt1=/10?/g; document.write(str.match(patt1));
<!--[if !supportLists]-->· <!--[endif]-->n{X} 匹配包含 X 个 n 的序列的字符串。
var str="100, 1000 or 10000?"; var patt1=/\d{4}/g; document.write(str.match(patt1));
<!--[if !supportLists]-->· <!--[endif]-->n{X,Y} 匹配包含 X 或 Y 个 n 的序列的字符串。
var str="100, 1000 or 10000?"; var patt1=/\d{3,4}/g; document.write(str.match(patt1));
<!--[if !supportLists]-->· <!--[endif]-->n{X,} 匹配包含至少 X 个 n 的序列的字符串。
var str="100, 1000 or 10000?"; var patt1=/\d{3,}/g; document.write(str.match(patt1));
<!--[if !supportLists]-->· <!--[endif]-->n$ 匹配任何结尾为 n 的字符串。
var str="Is this his"; var patt1=/is$/g; document.write(str.match(patt1));
<!--[if !supportLists]-->· <!--[endif]-->^n 匹配任何开头为 n 的字符串。
var str="Is this his"; var patt1=/^Is/g; document.write(str.match(patt1));
常见正则
<!--[if !supportLists]-->· <!--[endif]-->汉字: [\u4e00-\u9fa5]
<!--[if !supportLists]-->· <!--[endif]-->手机号: 1[0-9]{10}
<!--[if !supportLists]-->· <!--[endif]-->邮箱: (\S)+[@]{1}(\S)+[.]{1}(\w)+
相关推荐
如果你曾经用过Perl或任何其他内建正则表达式支持的语言,你一定知道用正则表达式处理文本和匹配模式是多么简单。如果你不熟悉这个术语,那么“正则表达式”(Regular Expression)就是一个字符构成的串,它定义了一...
正则表达式验证工具 V1.0 本软件主要用于检测正则表达式是否正确。 运行环境:本软件为绿色软件,无需安装,但需要Microsoft .NET Framework 4 支持,如果没有请前去下载(下载路径:...
《学习正则表达式》从正则表达式的基本概念讲起,到编写完整的sed和Perl脚本,再到转换HTML文件,将这种强大的工具解释得清晰透彻。...《学习正则表达式》适合对正则表达式感兴趣的程序员和互联网从业者。
正物色一本学习正则表达式的入门图书?恭喜,《学习正则表达式》非常适合你!本书提供大量经典简洁的示例,从零开始教你逐步掌握正则表达式。通过匹配特定单词、字符和模式,读者很快就可以自己动手使用正则表达式...
正则表达式正则表达式正则表达式正则表达式 验证程序
《正则表达式经典实例》讲解了基于8种常用的编程语言使用正则表达式的经典实例。书中提供了上百种可以在实战中使用的实例,以帮助读者使用正则表达式来处理数据和文本。对于如何使用正则表达式来解决性能不佳、误报...
让 Office Excel、WPS 表格支持正则表达式的免费插件:「Excel 正则工具」顾名思义,需要使用 “正则表达式(PCRE)” 的知识来进行操作,对于完全没有接触过正则的朋友也许有一定的入门门槛,但正则实在是太过强大...
正则表达式,又称规则表达式。(英语:Regular Expression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。正则表达式通常被用来检索、替换那些符合某个模式(规则)的文本。 许多程序设计语言都支持利用...
正则表达式整理汇总,含以下内容 [1]里面在包括一个正则表达式测试小程序 RegexTester.exe [2]正则表达式30分钟入门教程.pdf] [3]正则表达式规则.doc [4]正则表达式30分钟入门教程.mht 自己整理用的,现在...
常用正则表达式 正则表达式 常用正则表达式 正则表达式
常用正则表达式大全: 匹配中文字符的正则表达式 匹配空白行的正则表达式 匹配HTML标记的正则表达式 匹配Email地址的正则表达式 匹配网址URL的正则表达式 匹配国内电话号 匹配中国邮政编码 匹配身份证 匹配ip地址 ...
java,正则表达式,详解,java正则表达式,PDF
Microsoft®.NET Framework 对正则表达式的支持是一流的,甚至在 Microsoft® ASP.NET 中也有依赖正则表达式语言的控件。本文介绍了深入学习正则表达式的基础知识和推荐内容。 本文主要面向对正则表达式知之甚少或...
随着互联网的迅速发展,几乎所有工具软件和程序语言都支持的正则表达式也变得越来越强大和易于使用。本书是讲解正则表达式的经典之作。本书主要讲解了正则表达式的特性和流派、匹配原理、优化原则、实用诀窍以及调校...
Java正则表达式Java正则表达式Java正则表达式Java正则表达式
正则表达式日期校验 正则表达式日期校验 正则表达式日期校验
C语言正则表达式库,兼容perl的正则表达式库,使用简介、方便
非常棒的正则表达式速成教程,号称30分钟叫你学会正则表达式常用用法。我本人学了一下,感觉不赖,不过俺比较笨,30分钟的东西学了3个多小时,已经从什么都不会变成会编各种复杂的正则筛选条件了^^!这个教程是配合...
UE正则表达式、UE正则表达式
日期正则表达式(很实用) 日期正则表达式(很实用)日期正则表达式(很实用)日期正则表达式(很实用)