Regular Expression

推荐文章 http://www.cnblogs.com/rubylouvre/archive/2010/03/09/1681222.html

正则表达式,其实在以前学Linux的时候有学过一点,只是都忘记了, 正则表达式应该都是一样的语法。

Creating a Regular Expression

两种方法创建正则表达式:

正则字面量

var reg = /a+b/;

RegExp对象

var reg = new RegExp("a+b");

Special Characters

\ 转意,即通常在"\"后面的字符不按原来意义解释,如/b/匹配字符"b",当b前面加了反斜杆后/\b/,转意为

匹配一个单词的边界。 
-或- 
对正则表达式功能字符的还原,如"*"匹配它前面元字符0次或多次,/a*/将匹配a,aa,aaa,加了"\"后,/a\*/

将只匹配"a*"。 
^ 匹配一个输入或一行的开头,/^a/匹配"an A",而不匹配"An a" 
$ 匹配一个输入或一行的结尾,/a$/匹配"An a",而不匹配"an A" 
* 匹配前面元字符0次或多次,/ba*/将匹配b,ba,baa,baaa 
+ 匹配前面元字符1次或多次,/ba*/将匹配ba,baa,baaa 
? 匹配前面元字符0次或1次,/ba*/将匹配b,ba 
(x) 匹配x保存x在名为$1...$9的变量中 
xy 匹配x或y 
{n} 精确匹配n次 
{n,} 匹配n次以上 
{n,m} 匹配n-m次 
[xyz] 字符集(character set),匹配这个集合中的任一一个字符(或元字符) 
[^xyz] 不匹配这个集合中的任何一个字符 
[\b] 匹配一个退格符 
\b 匹配一个单词的边界 
\B 匹配一个单词的非边界 
\cX 这儿,X是一个控制符,/\cM/匹配Ctrl-M 
\d 匹配一个字数字符,/\d/ = /[0-9]/ 
\D 匹配一个非字数字符,/\D/ = /[^0-9]/ 
\n 匹配一个换行符 
\r 匹配一个回车符 
\s 匹配一个空白字符,包括\n,\r,\f,\t,\v等 
\S 匹配一个非空白字符,等于/[^\n\f\r\t\v]/ 
\t 匹配一个制表符 
\v 匹配一个重直制表符 
\w 匹配一个可以组成单词的字符(alphanumeric,这是我的意译,含数字),包括下划线,如[\w]匹配"$5.98"

中的5,等于[a-zA-Z0-9] 
\W 匹配一个不可以组成单词的字符,如[\W]匹配"$5.98"中的$,等于[^a-zA-Z0-9]

Working with Regular Expressions


Table 4.2 Methods that use regular expressions
MethodDescription
execA RegExp method that executes a search for a match in a string. It returns an array of information.
testA RegExp method that tests for a match in a string. It returns true or false.
matchA String method that executes a search for a match in a string. It returns an array of information or null on a mismatch.
searchA String method that tests for a match in a string. It returns the index of the match, or -1 if the search fails.
replaceA String method that executes a search for a match in a string, and replaces the matched substring with a replacement substring.
splitA String method that uses a regular expression or a fixed string to break a string into an array of substrings.

转载于:https://www.cnblogs.com/coolicer/archive/2011/04/23/2025458.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值