# Javascript正则表达式难点、重点

### 重复的贪婪性

`` {n,m}   {n,}   {n}   ?   +   *``
• ?表示匹配0次或1次，（最多匹配1次）；

• +表示匹配1次或多次，（最少匹配1次）；

• *表示匹配0次或多次，（匹配任意次）；

• {m,n}表示匹配m到n次，（最少m次，最多n次）；

``````console.log("@123@456@789".match(/\d{1,2}/g))
// ==> ["12", "3", "45", "6", "78", "9"]``````

{1,2}优先匹配2次（贪婪），还可以匹配1次，可以看到上面的结果为["12", "3", "45", "6", "78", "9"]；

``````console.log("@123@456@789".match(/\d{1,2}?/g))
// ==> ["1", "2", "3", "4", "5", "6", "7", "8", "9"]``````

### 分组

``````console.log("2016-10-15".match(/\d{4}[\/\-\.]\d{2}[\/\-\.]\d{2}/))
// ==> ["2016-10-15"]``````

``````console.log("2015-10-15".match(/(\d{4})[\/\-\.](\d{2})[\/\-\.](\d{2})/))
// ==> ["2016-10-15", "2016", "10", "15"]``````

``````console.log("2015-10-15".replace(/(\d{4})[\/\-\.](\d{2})[\/\-\.](\d{2})/g,"\$3-\$2-\$1"))
// ==> "15-10-2016"``````

``````var reg = /(?:\d{4})(\-|\.|\/)\d{2}\1\d{2}/;
console.log(reg.test('2016-10-15'));    // ==>true
console.log(reg.test('2016.10.15'));    // ==>true
console.log(reg.test('2016/10/15'));    // ==>true
console.log(reg.test('2016-10/15'));    // ==>false``````

``````console.log("1-2-3-4".replace(/(\d)/g,"x"));
// ==> x-x-x-x
console.log("1-2-3-4".replace(/(-\d)/g,"x"));
// ==> 1xxx
console.log("1-2-3-4".replace(/-(\d)/g,"x"));
// ==> 1xxx
console.log("1-2-3-4".replace(/(-\d)/g,"0\$1"));
// ==> 10-20-30-4
console.log("1-2-3-4".replace(/-(\d)/g,"0\$1"));
// ==> 1020304
console.log("1-2-3-4".match(/(-\d)/g));
// ==> ["-2", "-3", "-4"]
console.log("1-2-3-4".match(/-(\d)/g));
// ==> ["-2", "-3", "-4"]
console.log(RegExp.\$1);
// ==> 4``````

``````var date="2014-12-30 12:30:20";
console.log(date.replace(/(\d{4})-(\d{2})-(\d{2})/g,"\$2-\$3-\$1"));
// ==> "12-30-2014 12:30:20"
console.log(date.replace(/(\d{4})-(\d{2})-(\d{2})\s[\w+\:]{8}/g,"\$2-\$3-\$1"))
// ==> "12-30-2014"``````

### 向前查找 （此东西是个位置！）

``^ \$ \b \B (?=p) (?!p)``

(?=3)表示啥意思呢？表示3前面的位置，见例子：

``````console.log("a1b2c3d4".replace(/(?=3)/g,"x"));
// ==> a1b2cx3d4
console.log("a1b2c3d4".replace(/(?=\d)/g,"x"));
// ==> ax1bx2cx3dx4``````

``````console.log("a1b2c3d4".replace(/(?!3)/g,"x"));
// ==> xax1xbx2xc3xdx4x
console.log("a1b2c3d4".replace(/(?!\d)/g,"x"));
// ==> xa1xb2xc3xd4x``````

``````console.log("a1b2c3d4".replace(/\d(?=[bc])/g,"x"));
// ==> axbxc3d4
console.log("a1b2c3d4".match(/\d(?=[bc])/g));
// ==> ["1", "2"]``````

``````console.log("a1a2a3a4a5".replace(/a(?!3)/g,"x"));
// ==> x1x2a3x4x5``````

``````<div class="box"></div>
<script>
function getByClass(oParent, sClass) {
var tag = oParent.getElementsByTagName('*');
var arr = [];
var reg = new RegExp('\\b' + sClass + '\\b', "i");
for (var i = 0; i < tag.length; i++) {
if (reg.test(tag[i].className)) {
arr.push(tag[i])
}
}
return arr;
}
function findClassInElements(className, type) {
var ele = document.getElementsByTagName(type || '*');
var result = [];
var reg = new RegExp('(^|\\s)' + className + '(\\s|\$)');
for (var i = 0; i < ele.length; i++) {
if (reg.test(ele[i].className)) {
result.push(ele[i])
}
}
return result
}
console.log(getByClass(document.body, "box"));
console.log(findClassInElements('box'));
var box = findClassInElements('box')

box.forEach(function(value,index){
console.log(value);
})
</script>``````
431 声望
18 粉丝
0 条评论