代码改变世界

正则表达式

2011-12-02 14:01  NEVERGIVEUP-  阅读(130)  评论(0)    收藏  举报

正则表达式

创建RegExp 对象 直接量语法 /pattern/attributes var re2 = /ab/ig;
创建 RegExp 对象的语法 new RegExp(pattern, attributes); var re1 = new RegExp("ab", "ig")
pattern 参数 pattern 是一个字符串,指定了正则表达式的模式或其他正则表达式  
attributes 参数 attributes 是一个可选的字符串,包含属性 "g"、"i" 和 "m"  
i 执行对大小写不敏感的匹配。  
g 执行全局匹配(查找所有匹配而非在找到第一个匹配后停止)  
m 执行多行匹配。  
 
/n+/ 匹配任何包含至少一个 n 的字符串,等价于n{1,}.例如:pattern: /b+a/g - input: 'hubba and bubba'
/n*/ 匹配任何包含零个或多个 n 的字符串,等价于n{0,}. 例如: pattern: /b*a/g - input: 'hubba and bubba'.
/n?/ 匹配任何包含零个或一个 n 的字符串,等价于n{0,1}. 例如:pattern: /b?a/g - input: 'hubba and bubba'
/n{X}/ 匹配包含 X 个 n 的序列的字符串.例如:pattern: /9{2}/g - input: '79799799979999'
/n{X,Y}/ 匹配包含 X 或 Y 个 n 的序列的字符串.例如:pattern: /9{2,3}/g - input: '79799799979999'.
/n{X,}/ 匹配包含至少 X 个 n 的序列的字符串.例如:pattern: /9{2,}/g - input: '79799799979999'
/n$/ 匹配任何结尾为 n 的字符串,例如:/$m/不匹配'format',而匹配'form'
/^n/ 匹配任何开头为 n 的字符串,例如:/^m/不匹配'woman',而匹配'man'
/pattern1(?=pattern2)/ 匹配任何其后紧接pattern2的字符串pattern1,例如:Pattern: /a(?=b)/g - input: 'abracadabra'
/pattern1(?!pattern2)/ 匹配任何其后不接pattern2的字符串pattern1,例如:Pattern: /a(!=b)/g - input: 'abracadabra'
/pattern1|pattern2/ 匹配pattern1或pattern2,例如:pattern: /(a|r)/g - input: 'agreed'
 
/[axc]/ 查找方括号之间的任何字符
/[^adc]/ 查找任何不在方括号之间的字符
/[0-9]/ 查找任何从 0 至 9 的数字
/[a-z]/ 查找任何从小写 a 到小写 z 的字符
/[A-Z]/ 查找任何从大写 A 到大写 Z 的字符
/[a-Z]/ 查找任何从小写 a 到大写 Z 的字符
/[red|blue|green]/ 查找任何指定的选项
 
/./ 查找单个字符,除了换行和行结束符,语法:/regexp./; 例如: pattern:/h.t/g - input:"That's hot!"
/\w/ 查找单词字符。
/\W/ 查找非单词字符
/\d/ 查找数字--只匹配1位数
/\D/ 查找非数字字符
/\s/ 查找空白字符
/\S/ 查找非空白字符
/\b/ 查找位于单词的开头或结尾的匹配,语法:/\b regexp/,例如: pattern: /\ba/g - input: 'an abracadabra'.
/\B/ 查找不处在单词的开头或结尾的匹配,例如: pattern: /\Ba/g - input: 'an abracadabra'.
/\n/ 查找换行符, 等价于\x0a
/\f/ 查找换页符, 等价于\x0c
/\r/ 查找回车符, 等价于\x0d.
/\t/ 查找制表符, 等价于\x09
/\v/ 查找垂直制表符, 等价于\x0b
/\xxx/ 查找以八进制数 xxx 规定的字符
/\xdd/ 查找以十六进制数 dd 规定的字符。
/\uxxxx/ 查找以十六进制数 xxxx 规定的 Unicode 字符。
/\0/ 查找 NUL 字符
 
/abc/ Matches the first occurrence of the 'abc' string
/12/ Matches the first occurrence of the '12' string.
/ab*c/ Matches the first occurrence of the string that begins with 'a', followed by any number of 'b' and ends with 'c'. Possible strings: 'ac', 'abc', 'abbc', 'abbbc', ...
/ab+c/ Matches the first occurrence of the string that begins with 'a', followed by at least one 'b' and ends with 'c'. Possible strings: 'abc', 'abbc', 'abbbc', ...
/ab{2,}c/ Matches the first occurrence of the string that begins with 'a', followed by two or more 'b' and ends with 'c'. Possible strings: 'abbc', 'abbbc', 'abbbbc', ...
/(ab)*c/ Matches the first occurrence of the string that begins with some 'ab', followed by 'c'. Possible strings: 'c', 'abc', 'ababc', 'abababc', ...
/(ab)+c/ Matches the first occurrence of the string that begins with one or more 'ab', followed by 'c'. Possible strings: 'abc', 'ababc', 'abababc', ...
/(ab){2,}c/ Matches the first occurrence of the string that begins with two or more 'ab', followed by 'c'. Possible strings: 'ababc', 'abababc', 'ababababc', ...
/ab?c/ Matches the first occurrence of the string that begins with 'a', followed by at most one 'b' and ends with 'c'. Possible strings: 'ac' 'abc'.
/ab{1,2}c/ Matches the first occurrence of the string that begins with 'a', followed by one or two 'b' and ends with 'c'. Possible strings: 'abc' 'abbc'.
/ab{2}c/ Matches the first occurrence of the 'abbc' string.
/(ab)?c/ Matches the first occurrence of the string that begins at most one 'ab', followed by 'c'. Possible strings: 'c' 'abc'.
/(ab){1,2}c/ Matches the first occurrence of the string that begins with one or two 'ab', followed by 'c'. Possible strings: 'abc' 'ababc'
/(ab){2}c/ Matches the first occurrence of the 'ababc' string.
/^whole$/ Checks if the analyzed string is 'whole'.
/^ca(t|r)$/ Checks if the analyzed string is 'cat' or 'car'
/[abc]/ Matches the first occurrence of the 'a', 'b' or 'c' character.
/[ab]{1,2}/ Matches the first occurrence of the 'a', 'b', 'aa', 'ab', 'ba' or 'bb' string.
/[^abc]/ Matches the first occurrence of a character that is not 'a', 'b' and 'c'.
/\d/ Matches the first occurrence of any digit.
/\d{1,2}/ Matches the first occurrence of one or two digits.
/\d|[1-9]\d/ Matches the first occurrence of an integer between 0 and 99. Similar to the previous regular expression, but '00', '01', '02', ..., '09' are not allowed.
/(\+|-)?(\d|[1-9]\d)/ Matches the first occurrence of an integer between 0 and 99 with or without sign.
/\sabc/ Matches the first occurrence of the string that begins with a whitespace, followed by 'abc'
/\sabc\s/ Matches the first occurrence of the string that begins with a whitespace, followed by 'abc' and end s with a whitespace.
/\s\S+\s/ Matches the first occurrence of the string that begins with a whitespace, followed by at least one non-whitespace character and ends with a whitespace.
 
RegExp方法 方法 语法 描述 返回值
compile RegExpObject.compile(regexp,modifier) 编译正则表达式  
exec RegExpObject.exec(string) 检索字符串中指定的值。 返回一个数组,其中存放匹配的结果。如果未找到匹配,则返回值为 null。
test RegExpObject.test(string) 检索字符串中指定的值。 找到就返回true,否则false

 

 

转载http://www.w3cfuns.com/thread-2890-1-1.html