正则表达式规则见:http://www.cnblogs.com/wishyouhappy/p/3756812.html,下面说明相关方法
String相关方法
概括:
search()
replace()
match()
split()
1. search(RegExp)
- 返回第一个与之匹配的子串的起始位置,找不到子串时返回-1
- 如果search的参数不是正则表达式,则会通过RegExp将其转化为正则表达式
- search不支持全局检索g,因而即使加了也会忽略g
eg:
console.log( "Javascript".search(/script/i)); //4
2. replace(RegExp, String)
- 用以检索和替换,首先会对字符串使用RegExp检索,然替换字符串
- 如果加了g则所有的替换,如果不加g则只替换第一个
- 如果第一个参数是字符串,则之间检索,而不是转换为RegExp区别与search()
- 使用$加数字可以指定子表达式相匹配的文本的替换
eg1:
var sentence = "javascript is really interesting";
console.log(sentence.replace(/javascript/gi,"Javascript")); //Javascript is really interesting
eg2:将英文引号转化为中文引号
var sentence = "\"javascript is really interesting\"";
var quote = /"([^"]*)"/g;
console.log(sentence);
console.log(sentence.replace(quote,'“$1”'));
输出:
"javascript is really interesting"
“javascript is really interesting”
var sentence = "\"javascript is really interesting\"";
var quote = /"([^"]*)"/g;
console.log(sentence);
console.log(sentence.replace(quote,'hhh$1kkk')); 输出
"javascript is really interesting" VM93:4
hhhjavascript is really interestingkkk
3. match(RegExp)
- 返回由匹配结果组成的数组
- 如果加g全局检索,如果不加g只检索第一个匹配
- 即使不进行全局检索,返回的也是一个数组,数组第一个元素是匹配的字符串,余下的元素是圆括号括起来的子表达式
- 当传入非全局表达式时等价于exec()方法,下面RegExp的方法中介绍
- 没有匹配时返回null
eg1:
var sentence = "javascript is really interesting,java is also an interesting language, java and javascript have many similarities"; console.log(sentence.match(/java+[a-z]*/gi)); 输出:
["javascript", "java", "java", "javascript"]
eg2:解析网址
var urlPattern = /(\w+):\/\/([\w.]+)\/(\S*)\/([\w\S.]+html)/;
var url = "visit my blog at http://www.cnblogs.com/wishyouhappy/p/articles~.html";
var result = url.match(urlPattern);
if(result != null){
var fullUrl = result[0];
var protocol = result[1];
var host = result[2];
var path = result[3];
var file = result[4];
console.log(fullUrl);
console.log(protocol);
console.log(host);
console.log(path);
console.log(file);
}
输出:
http://www.cnblogs.com/wishyouhappy/p/articles~.html
http
www.cnblogs.com
wishyouhappy/p
articles~.html
4. split()
- 将字符串拆分为字符串数组
- 参数可以是字符串或者正则表达式
eg:
console.log("22,33,44".split(","));
console.log("22 , 33, 44 ".split(","));
console.log("22 , 33, 44".split(/\s*,\s*/)); 输出:
["22", "33", "44"]
["22 ", " 33", " 44 "]
["22", "33", "44"]
RegExp对象方法
RegExp有5个属性如下:
source: 正则表达式文本
global:对应g
ignoreCase:对应i
multiline:对应m
lastIndex:如果带有g属性,该值存储下一次检索的开始位置
1. 构造方法RegExp(String,String)
注意:1.当给RegExp()传入一个正则表达式时如果有\作为转义字符,则需用\替换为\\
2. 第二个参数是可选的,但是第二个参数只能传入修饰符g、i、m或是它们的组合
eg:
var zipCode = new RegExp("\\d{5}","gm");
2.exec()
- 使用类似string的match()方法,在字符串中进行匹配检索只是传入的参数是相反的,match()传入RegExp,exce传入string
- 没有匹配时返回null,有匹配时返回数组和match中的非全局匹配类似
- 属性Index包含发生匹配的字符位置
- 如果没有检索结果lastIndex会重置为0
eg:
var urlPattern = /(\w+):\/\/([\w.]+)\/(\S*)\/([\w\S.]+html)/;
var url = "visit my blog at http://www.cnblogs.com/wishyouhappy/p/articles~.html";
var result = urlPattern.exec(url);
if(result != null){
var fullUrl = result[0];
var protocol = result[1];
var host = result[2];
var path = result[3];
var file = result[4];
console.log(fullUrl);
console.log(protocol);
console.log(host);
console.log(path);
console.log(file);
} //输出
http://www.cnblogs.com/wishyouhappy/p/articles~.html VM49:11
http
www.cnblogs.com
wishyouhappy/p
articles~.html
3.test()
等价于exec,exec返回null时test()对应为false,否则为true
eg:
var urlPattern = /(\w+):\/\/([\w.]+)\/(\S*)\/([\w\S.]+html)/;
var url = "visit my blog at http://www.cnblogs.com/wishyouhappy/p/articles~.html";
console.log( urlPattern.test(url)); //true
注意事项
String的4个方法不会用到lastIndex属性,exec和test需要用到,如果设置了g属性则要么找出所有的匹配然后自动将lastIndex置为0要么手动置为0