`
txf2004
  • 浏览: 6876414 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Javascript 正则表达式 - 马永占 译

阅读更多

版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章原始出版、作者信息和本声明。否则将追究法律责任。http://blog.csdn.net/mayongzhan - 马永占,myz,mayongzhan

原文地址:http://www.thespanner.co.uk/2008/02/01/javascript-regular-expressions/

我和Ronald讨论Javascript和PHP的正则表达式.他过去一直用的是PHP的preg,对Javascript的正则比较陌生,所以我就分享了我的script知识.

首先,PHP中的preg_match和Javascript中的match有相同的功能,他们除了语法之外其他的都非常相似.在Javascript中match是字符串对象(就是一个字符串)的一部分,下面是match的用法:



alert('Test'.match(/[a-z]/))



你也可以把match用在子正则匹配上,如下(我尽量把这些写的类似于PHP):-



pattern = /([a-z]+)([0-9]+)([A-Z]+)/;
subject = 'test1234TEST';
matches = subject.match(pattern);
match1 = matches[1];
match2 = matches[2];
match3 = matches[3];
alert(match1);
alert(match2);
alert(match3);



你也可以用RegExp创建匹配式,这样有利于传递变量到匹配式中,而且不需要"//"



a = 'a+';
alert(new RegExp(a).exec('123aaaabcdef'));



exec方法也可以被"//"这样的匹配式使用.



alert((/[0-9]+/).exec('12345abcdef'))



Javascript支持像"g","i"和"m"的修正符,如下:



matches = 'ababababababa'.match(/[a]/g);
alert



修改 by Planet PHP, 更多: the original (4722 bytes)







Ronald and I had a good conversation about Javascript regular expressions comparing them to PHP. He was having difficultly with the syntax because he was used to preg in PHP so I promised to share my knowledge gained from developing various online scripts.
First up preg_match in PHP can be achieved using the match function in Javascript, they are both very similar but it’s just a matter of getting your head round the different syntax. Match is part of the String object and here is how to use it:-
alert('Test'.match(/[a-z]/))
You can match subpatterns like this (I’ve tried to keep it close to PHP syntax as possible):-
pattern = /([a-z]+)([0-9]+)([A-Z]+)/;
subject = 'test1234TEST';
matches = subject.match(pattern);
match1 = matches[1];
match2 = matches[2];
match3 = matches[3];
alert(match1);
alert(match2);
alert(match3);
You can also create matches using the RegExp object, this is useful for passing variables into the pattern which to my knowledge isn’t possible with “//” syntax.
a = 'a+';
alert(new RegExp(a).exec('123aaaabcdef'));
The exec method can also be called from “//” patterns.
alert((/[0-9]+/).exec('12345abcdef'))
Javascript supports the modifiers “g”, “i” and “m”, here’s how to use them with “//” syntax:-
matches = 'ababababababa'.match(/[a]/g);
alert
Truncated by Planet PHP, read more at the original (another 4722 bytes)

分享到:
评论

相关推荐

    正则表达式-JavaScript字符校验方法

    正则表达式-JavaScript字符校验方法

    javascript正则表达式迷你书 (1).pdf

    javascript正则表达式迷你书 (1).pdf

    JavaScript正则表达式.ppt

    了解正则表达式概念 掌握正则表达式的语法 熟练掌握正则表达式在JavaScript中的应用

    常用java正则表达式

    许多语言,包括Perl、PHP、Python、JavaScript和JScript,都支持用正则表达式处理文本,一些文本编辑器用正则表达式实现高级“搜索-替换”功能。那么Java又怎样呢?本文写作时,一个包含了用正则表达式进行文本处理...

    javascript正则表达式详解 (chm)

    javascript正则表达式详解 (chm)

    常用Javascript正则表达式汇总

    常用Javascript正则表达式汇总,常用Javascript正则表达式汇总

    Javascript正则表达式教程

    网上看到的一篇介绍Javascript正则表达式的文章,感觉非常不错,整理了一下导出成PDF,有兴趣的看一下。文章属于转载,文档中注明了出处。

    精通 JavaScript正则表达式

    正则表达式可以: •测试字符串的某个模式。例如,可以对一个输入字符串进行测试,看在该字符串是否存在一个电话号码模式或一个信用卡号码模式。这称为数据有效性验证 •替换文本。可以在文档中使用一个正则表达式...

    javascript正则表达式详解

    javascript正则表达式详解javascript正则表达式详解javascript正则表达式详解javascript正则表达式详解javascript正则表达式详解javascript正则表达式详解javascript正则表达式详解javascript正则表达式详解...

    JavaScript正则表达式迷你书

    JavaScript正则表达式迷你书,学习javascript的宝典。

    使用正则表达式的模式匹配

    JavaScript的RegExp类表示正则表达式,而String和RegExp都定义了使用正则表达式进行强大的模式匹配和文本检索与替换的函数。 ECMAScript v3对JavaScript正则表达式进行了标准化。JavaScript 1.2实现了ECMAScript v3...

    javascript常用正则表达式大全

    javascript常用正则表达式大全,基本覆盖基本需求的正则表达式

    JavaScript验证正则表达式大全.txtJavaScript验证正则表达式大全.txt

    JavaScript验证正则表达式大全.txtJavaScript验证正则表达式大全.txtJavaScript验证正则表达式大全.txtJavaScript验证正则表达式大全.txtJavaScript验证正则表达式大全.txtJavaScript验证正则表达式大全....

    JavaScript正则表达式经典程

    JavaScript正则表达式经典程JavaScript正则表达式经典程JavaScript正则表达式经典程JavaScript正则表达式经典程JavaScript正则表达式经典程

    正则表达式经典实例

    对于如何使用正则表达式来解决性能不佳、误报、漏报等常见的错误以及完成一些常见的任务,《正则表达式经典实例》给出了涉及基于C#、Java、JavaScript、Perl、PHP、Python、Ruby和VB.NET等编程语言的解决方案。...

    javascript 正则表达式实用版

    javascript 正则表达式实用版javascript 正则表达式实用版javascript 正则表达式实用版javascript 正则表达式实用版javascript 正则表达式实用版

    经典JavaScript正则表达式实战

    经典JavaScript正则表达式实战 目录 1. 正则表达式实战...1 2. 匹配结尾的数字...2 3. 统一空格个数...3 4. 判断字符串是不是由数字组成...3 5. 电话号码正则...3 6. 手机号码正则表达式...4 7. 使用正则...

    JavaScript正则表达式匹配 div style标签

    主要介绍了JavaScript正则表达式匹配<div><style>标签 的相关资料,需要的朋友可以参考下

    Regulex-JavaScript正则表达式解析和可视化工具

    Regulex是一款JavaScript正则表达式解析和可视化工具。通过该工具可以对任何正则表达式进行解析,并以可视化图表的显示显示该正则表达式的解析流程结构,还可以将图表导出为图片。

    JavaScript正则表达式(下)

    JJavaScript正则表达式下——相关方法 search match replace

Global site tag (gtag.js) - Google Analytics