1、test() 方法用于检测一个字符串是否匹配某个模式,如果字符串中含有匹配的文本,则返回 true,否则返回 false。
var patt = /e/;
patt.test("The best things in life are free!"); //字符串中含有 "e",所以该实例输出为:true
等效于 /e/.test("The best things in life are free!")
var reg = /(\d{4})-(\d{2})-(\d{2})/;
var dateStr = '2018-04-18'; reg.test(dateStr); //true
2、replace() 字符串替换
使用正则表达式且不区分大小写将字符串中的 Microsoft 替换为 Runoob
var txt = str.replace(/microsoft/i,"Runoob"); replace() 方法使用正则表达式
var txt = str.replace("Microsoft","Runoob"); replace() 方法使用字符串