正则表达式

===========================[  ]方括号 ====================

res = /[abc]/.test('la');          //t 查找方括号之间的任何字符。
res = /[^abc]/.test('lue');            //t查找任何不在方括号之间的字符。
res = /[0-9]/.test('this is a test');     //f 查找任何从 0 至 9 的数字。
res = /[a-z]/.test('234235453245');     //f 查找任何从小写 a 到小写 z 的字符。
res = /php|javascript|ios/i.test('PHP');      //t 查找任何指定的选项。
console.log(res);

===========================元字符====================

res = /./.test(' ');       //false 查找单个字符,除了换行和行结束符。 ==
res = /./.test('this is a test');
res = /w/.test('hello nana')   //true 查找单词字符。也就是查找[a-zA-Z0-9]
res = /w/.test('!#@w');      //true 里面有w
res = /W/.test('!#%');      //true [^a-zA-Z0-9]


var str = null; 
res = //.test(str);     //false


res = /S/.test(' ');       //false 查找非空白字符。
res = /go/.test('good');      //true 匹配单词边界 ,b在左匹配开头,在右匹配结尾
res = /o/.test('good');        //false
res = /d/.test('good');     //true
res = /Bd/.test('good');         //true B不区分左右 
console.log(res);

===========================量词====================
res = /o+/.test('google');      //true  匹配任何包含至少一个o 的字符串。
res = /o*/.test('google');      //true   匹配任何包含零个或多个 o 的字符串。
res = /o?/.test('google');      //true  匹配任何包含零个或一个 o 的字符串。
res = /o{2}/.test('goooogle');    //true   匹配包含 2 个 o 的序列的字符串。
res = /o{1,3}/.test('gogle');       //true   匹配包含 1 至 3 个 o 的序列的字符串。
res = /^k/.test('king');        //true    匹配任何开头为 k 的字符串。
res = /i$/.test('mai');        //true  匹配任何结尾为 i 的字符串。
res = /o(?=r)/.test('helloworld');      //true  匹配(o)任何其后紧接指定字符串 r 的字符串。
res = /o(?!r)/.test('hellowo/wld');    //true   匹配任何其后没有紧接指定字符串 n 的字符串。
res = /d/.test('aajk1dsfj');      //[0-9]   查找数字。
res = /D/.test('sdfkjllsdfj');      //[^0-9]   查找非数字字符。
console.log(res);

res = /is/i.exec('this is a test');
console.log(res);           //is 检索字符串中指定的值。返回找到的值,并确定其位置。0: "is" index: 2
console.log(res[0]);        //is


var str = 'this is a test is hello nana hello world';
var patt = /i/ig;
var myArr; 
// console.log(patt.exec(str)); //如果不注释此行,那么执行完此行后,直接执行下个:本次匹配下标5、
while((myArr = patt.exec(str)) !== null) {
  var msg = '找到了' + myArr[0] + ',本次匹配下表标为'+ myArr["index"];
  msg += '下一个匹配从' + patt.lastIndex;
  console.log(msg);
}

=========================支持正则表达式的string对象方法========================

match --》找到一个或多个正则表达式的匹配。

var str = 'this is a test';
res = str.match(/IS/i); 
console.log(res);         //is /放要找的内容/   i代表不区分大小写

res = str.match(/IS/ig);
console.log(res);         //is is //is /放要找的内容/  i代表不区分大小写 g代表全局搜索

search --》 检索与正则表达式相匹配的值。 

res = str.search(/is/i);        //2     is的下标 search 返回的是下标
console.log(res);

var str1 = str.replace(/is/ig, '!');   //th! ! a test 全局且不区分大小写的情况下,找到所有的is并替换为!
console.log(str1);

replace --》替换与正则表达式匹配的子串

var str = '2015-09-27';
res = str.replace(/(d{4})-(d{2})-(d{2})/, '$2/$3/$1');
console.log(res);          //替换位置

str = 'Hello nana Edu';
res = str.replace(/[A-Z]/g, func);
function func(match) {
return 'King_' + match.toLowerCase();
}
console.log(res);

split --》把字符串分割为字符串数组。

res = str.split("");
console.log(res);        //t,h,i,s, ,i,s, ,a, ,t,e,s,t

找到一个或多个正则表达式的匹配。

原文地址:https://www.cnblogs.com/18306815575z/p/10301086.html