js 正则

2019-01-17  本文已影响14人  McDu
  1. 有效数字:var reg = /^[+-]?(\d|([1-9]\d+))(\.\d+)?$/
  2. [] 中的所有字符代表本身意思
  3. 分组:改变 x|y 默认的优先级
  4. 全局修饰符解决懒惰性
var reg  = /\d+/g;
var str = "dfsdf1232fjskdjf21ekeke323";
var arr = [];
var res = reg.exec(str);
while(res){
  arr.push(res[0]);
  res = reg.exec(str);
}
console.log(arr);
// ["1232", "21", "323"]
  1. 每次捕获截取最长的值,在量词元字符后面加一个 即可。
    问号:
var reg = /\d+?/g;
var str = "fef23fsj23";
var arr = [], res= reg.exec(str);
while(res){
  arr.push(res[0]);
  res = reg.exec(str);
}
console.log(arr);
// ["2", "3", "2", "3"]
  1. 字符串中的 match 方法,把所有和正则匹配的字符都获取到。(没有g修饰符的时候和 exec 用法一样)
var reg = /\d+?/g;
var str = "fejf323fjisj323";
var arr = str.match(reg);
console.log(arr); 
//["3", "2", "3", "3", "2", "3"]
  1. 当大正则中存在分组时,match 不能匹配。
  2. 分组:改变优先级,分组引用
var reg = /^\w\w$/;   // 两个字符(数字、字母、下划线)
var reg = /^(\w)\1(\w)\2$/;
reg.test("ddff");  // true;
reg.test("dd0f");  //  false 值不一样
// \2: 代表和第二个分组出现一样的内容
// \1: 代表和第一个分组出现一样的内容

分组捕获,正则在捕获时,不仅把大正则的内容捕获到,也把小分组的内容捕获到

var reg = /^(\d{2})(\d{4})(\d{4})(\d{2})(\d{2})(\d{2})(\d)(\d|X)$/;
var str = "141726199802123202";
var arr = reg.exec(str);

// ["141726199802123202", "14", "1726", "1998", "02", "12", "32", "0", "2", index: 0, input: "141726199802123202"]
// arr[0] 是大正则匹配的内容
// arr[1] 是第一个分组捕获的内容
// .....
var reg = /^(\d{2})(\d{4})(\d{4})(\d{2})(\d{2})(?:\d{2})(\d)(?:\d|X)$/;
var str = "141726199802123202";
var arr = reg.exec(str);
// ?:  的意思是只匹配不捕获
// ["141726199802123202", "14", "1726", "1998", "02", "12", "0", index: 0, input: "141726199802123202"]

console.log(str.match(reg))
// ["141726199802123242", "14", "1726", "1998", "02", "12", "4", index: 0, input: "141726199802123242"]

var reg = /fish(\d+)/g;
var str = "fish3223fish4343fish23";
console.log(reg.exec(str));
// ["fish3223", "3223", index: 0, input: "fish3223fish4343fish23"]
console.log(reg.exec(str));
// ["fish4343", "4343", index: 8, input: "fish3223fish4343fish23"]
console.log(reg.exec(str));
// ["fish23", "23", index: 16, input: "fish3223fish4343fish23"]

console.log(str.match(reg));
// match 只捕获大正则匹配的内容
// ["fish3223", "fish4343", "fish23"]
  1. 字符串中的replace:
var str = "dao333dao333dao333";
str = str.replace("dao","ben")
console.log(str);
// replace 每次只替换一个
// "ben333dao333dao333"

str = str.replace(/dao/g,"ben")
// "ben333ben333ben333"
var str = "dao222dao222dao222"
str = str.replace(/dao/g,function(){
  console.log(arguments);
  return "pen";
})
// 函数执行3次 , 每次 arguments 的值跟执行 exec 函数返回的值一样。
// return : 返回的结果是什么,相当于把当前大正则捕获到的内容替换。
str = str.replace(/(d+)/g,function(){
  console.log(arguments[1]);
// arguments[1] 获取第一个分组中的内容
//等效于:console.log(RegExp.$1) 
  return "pen";
})
var str = "1013222";
var arr = ["一","二","三","四","五"];
str = str.replace(/\d/g,function(){
  return arr[arguments[0]];
})
// "二一二四三三三"

获取一个字符串中出现次数最多的字符,并获取出现次数

var str = "feijfoirjfoifffsd";
var hash = {};
str.replace(/[a-z]/gi, function(){
  var val = arguments[0];
  hash[val]= hash[val]?++hash[val]:1;
})
console.log(hash);
var arr = '';
var max = 0;
for(var k in hash){
  max = Math.max(hash[k],max);
}
for(var k in hash){
  hash[k] === max ? arr = k:null;
}
console.log(arr);
console.log(max)
var str = "my name is {0}, two is {1}, three is {2} ";
var arr = ['bob', 12, "Fiey"];

str = str.replace(/{(\d+)}/g, function(){
    return arr[arguments[1]];
})
console.log(str);
// my name is bob, two is 12, three is Fiey
var str = "http://www.jianshu.com/u/3255897b63bf?name=eee&abc=212&age=21";
var obj = {
  name:"eee",
  abc:"212",
  app:"21"
};
var reg = /([^?=&]+)=([^?=&]+)/g;

var obj1 = {};
str.replace(reg,function(){
  obj1[arguments[1]] = arguments[2];
})
var obj2 = {};
var res = reg.exec(str);
while(res){
  obj2[res[1]] = res[2];
  res = reg.exec(str);
}
console.log(obj2);
// {name: "eee", abc: "212", age: "21"}
var str = "2017-8-25 14:32:01";
var a = new Date(str);
// var a = new Date(str.replace('-','/').replace('-','/')) ; 兼容ie

var regStr = "{0}年{1}月{2}日 {3}时{4}分{5}秒";
var arr = ["2017","8","25","14","32","1"];

var reg = /{(\d+)}/g;
// reg.exec(regStr)
// ["{0}", "0", index: 0, input: "{0}年{1}月{2}日 {3}时{4}分{5}秒"]
regStr = regStr.replace(reg,function(){
  var n = arguments[1], val = arr[n];
  val.length == 1 ? val = "0" + val : void 0;
   return  val;
});

var reg2 = /^(\d{4})[-/](\d{1,2})[-/](\d{1,2}) +(\d{1,2}):(\d{1,2}):(\d{1,2})$/;
var arr2 = [];
str.replace(reg2,function(){
  arr2 = [].slice.call(arguments);
  arr2 =  arr2.slice(1,6);
  return arr2;
})

arguments 每次把正则匹配到的内容捕获到,如果没有分组,一般是三个参数,如果有分组,从 arguments[1],开始时分组的内容。

上一篇下一篇

猜你喜欢

热点阅读