虽然一直在使用正则表达式,可是一直没有系统的概括。如下从正则表达是的功能进行相应的介绍。若有不正确的地方,望请纠正。正则表达式
模式检测是指要检测的文本是否符合咱们预期的模式,主要用于作登陆、注册的验证等:如,咱们常常在注册时要求帐号长度为6-16位等,如下是经常使用正则表达式spa
2. 文本内容的部分替换code
1 var str="Our life is bright,we should cherish our life"; 2 var pattern=/life/g; 3 var result=str.replace(pattern,"future"); 4 console.log(result);
1 var str="Our life is bright,we should cherish our life"; 2 var pattern=/\b\w+\b/g; 3 var result=str.replace(pattern,function(word){ 4 return word.substring(0,1).toUpperCase()+word.substring(1); 5 }); 6 7 console.log(result);//Our Life Is Bright,We Should Cherish Our Life
3. 得到模式匹配的文本blog
1 var str="Our life is bright,we should cherish our life"; 2 var pattern=/li\w*/g; 3 var result=str.match(pattern); 4 console.log(result);//["life", "life"]
1 var str="Our life is bright,we should cherish our life"; 2 var pattern=/li\w*/g; 3 var result=pattern.exec(str); 4 console.log(result);//["life", index: 4, input: "Our life is bright,we should cherish our life"]