小白教程
所有教程
关于
Search
172.70.135.104
172.70.135.104
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“JavaScript RegExp 对象”的源代码
本页内容
上一节:
JavaScript_Math(算数)对象
下一节:
JavaScript_Window
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:JavaScript RegExp 对象}}[[Category:Javascript 教程|65]] = JavaScript RegExp 对象 = RegExp:是正则表达式(regular expression)的简写。 == 完整 RegExp 对象参考手册 == 请查看我们的 [[jsref obj regexp|JavaScript RegExp 对象的参考手册]],其中提供了可以与字符串对象一同使用的所有的属性和方法。 这个手册包含的关于每个属性和方法的用法的详细描述和实例。 == 什么是 RegExp? == 正则表达式描述了字符的模式对象。 当您检索某个文本时,可以使用一种模式来描述要检索的内容。RegExp 就是这种模式。 简单的模式可以是一个单独的字符。 更复杂的模式包括了更多的字符,并可用于解析、格式检查、替换等等。 您可以规定字符串中的检索位置,以及要检索的字符类型,等等。 == 语法 == <pre> var patt=new RegExp(pattern,modifiers); 或更简单的方法 var patt=/pattern/modifiers; </pre> * 模式描述了一个表达式模型。 * 修饰符(modifiers)描述了检索是否是全局,区分大小写等。 <blockquote>'''注意:'''当使用构造函数创造正则对象时,需要常规的字符转义规则(在前面加反斜杠 \)。比如,以下是等价的: <pre> var re = new RegExp("\\w+"); var re = /\w+/; </pre></blockquote> == RegExp 修饰符 == 修饰符用于执行不区分大小写和全文的搜索。 '''i''' - 修饰符是用来执行不区分大小写的匹配。 '''g''' - 修饰符是用于执行全文的搜索(而不是在找到第一个就停止查找,而是找到所有的匹配)。 == 实例 1 == 在字符串中不区分大小写找"xiaobai" <sample title="" desc="" lang="html" hererun="1"> var str = "Visit xiaoBai"; var patt1 = /xiaobai/i; </sample> 以下标记的文本是获得的匹配的表达式: <sample title="" desc="" lang="html" hererun="1"> Visit xiaoBai </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <script> var str = "Visit xiaoBai"; var patt1 = /xiaobai/i; document.write(str.match(patt1)); </script> </body> </html> </run> == 实例 2 == 全文查找 "is" <sample title="" desc="" lang="html" hererun="1"> var str="Is this all there is?"; var patt1=/is/g; </sample> 以下标记的文本是获得的匹配的表达式: <sample title="" desc="" lang="html" hererun="1"> Is this all there is? </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <script> var str="Is this all there is?"; var patt1=/is/g; document.write(str.match(patt1)); </script> </body> </html> </run> == 实例 3 == 全文查找和不区分大小写搜索 "is" <sample title="" desc="" lang="html" hererun="1"> var str="Is this all there is?"; var patt1=/is/gi; </sample> 以下 标记的文本是获得的匹配的表达式: <sample title="" desc="" lang="html" hererun="1"> Is this all there is? </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <script> var str="Is this all there is?"; var patt1=/is/gi; document.write(str.match(patt1)); </script> </body> </html> </run> == test() == test()方法搜索字符串指定的值,根据结果并返回真或假。 下面的示例是从字符串中搜索字符 "e" : <sample title="" desc="" lang="html" hererun="1"> var patt1=new RegExp("e"); document.write(patt1.test("The best things in life are free")); </sample> 由于该字符串中存在字母 "e",以上代码的输出将是: <sample title="" desc="" lang="html" hererun="1"> true </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <script> var patt1=new RegExp("e"); document.write(patt1.test("The best things in life are free")); </script> </body> </html> </run> 当使用构造函数创造正则对象时,需要常规的字符转义规则(在前面加反斜杠 \) <sample title="" desc="" lang="html" hererun="1"> var re = new RegExp("\\w+"); </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <script> var str = 'xiaobai'; var patt1 = new RegExp('\\w', 'g'); // 有转义作为正则表达式处理 var patt2 = new RegExp('\w', 'g'); // 无转义作为字符串处理 var patt3 =/\w+/g; // 与 patt1 效果相同 document.write(patt1.test(str)) //输出 true document.write("<br>") document.write(patt2.test(str)) //输出 false document.write("<br>") document.write(patt3.test(str)) //输出 true </script> </body> </html> </run> == exec() == exec() 方法检索字符串中的指定值。返回值是被找到的值。如果没有发现匹配,则返回 null。 下面的示例是从字符串中搜索字符 "e" : == 实例 1 == <sample title="" desc="" lang="html" hererun="1"> var patt1=new RegExp("e"); document.write(patt1.exec("The best things in life are free")); </sample> 由于该字符串中存在字母 "e",以上代码的输出将是: <sample title="" desc="" lang="html" hererun="1"> e </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <script> var patt1=new RegExp("e"); document.write(patt1.exec("The best things in life are free")); </script> </body> </html> </run>
返回至“
JavaScript RegExp 对象
”。
上一节:
JavaScript_Math(算数)对象
下一节:
JavaScript_Window