週末に正則を復習しました.
35097 ワード
// JS , , , 。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
</head>
<script>
window.onload = function() {
var arrDiv = Array.prototype.slice.call(document.querySelectorAll("div")),
len = arrDiv.length;
while( len ) {
len--;
window[arrDiv.id] = arrDiv[len]
};
};
</script>
<body>
// ;
<script>
var str = "sdfsdf2,2424sdfsdf00234";
var arrResult = [];
for(var i= 0, len = str.length ;i < len ; i++) {
if( Number(str[i]) != Number(str[i]) )
continue;
arrResult.push( str[i] );
};
console.log(" " , arrResult );
var arr = str.split(/[\D]?/gi);
arr = arr.filter(function(e){
return e !== "";
});
/*
arr.ap(function(e){
return e !== "";
});
*/
console.log(" ",arr );
console.log(" ; ");
console.log( str.match(/\d+/g) );
console.log(" !");
var arrResult = [],
tmp = [];
for(var i= 0, len = str.length ;i < len ; i++) {
if( Number(str[i]) != Number(str[i]) )
continue;
tmp.push( str[i] );
if( Number(str[i+1]) !== Number(str[i+1]) ) {
arrResult.push( tmp.join("") );
tmp = [];
};
};
console.log( arrResult );
</script>
// HTML ;
<div id="reg">
<span>
sdfsd;
<span>sdfsdfsdf</span>
</span>
</div>
<script>
var eEl = document.getElementById("reg"),
reg = /<[^<]+>/gi;
// ;
console.log( eEl.innerHTML );
console.log( eEl.innerHTML.replace(reg,"") );
</script>
// ;
<input>
<br>
<input>
<script>
var aInpts = document.querySelectorAll("input"),
regC = /[^\u4e00-\u9fa5]/gi;
aInpts[0].addEventListener("input",function() {
aInpts[1].value = this.value.replace(regC,function($$) {
return "";
});
}, false);
</script>
<div id="www">
<span> :</span>
<input id="www1">
<br>
<span> :</span>
<div id="www2"></div>
</div>
<script>
var reg = /[a-zA-Z]+:\/\/[^\s]*/gi;
www1.addEventListener("input",function() {
www2.innerHTML = reg.test( this.value );
});
</script>
<div id=" QQ ">
<span> QQ :</span>
<input id="qq_input">
<br> QQ ;
<span id="is_qq"></span>
</div>
<script>
var regQ = /^[1-9][0-9]{4,10}$/g;
qq_input.addEventListener("input",function() {
is_qq.innerHTML = regQ.test(this.value)
});
</script>
<div id="date">
<span> - .</span>
<br>
<input id="data1">
<br>
<div id="data_result"></div>
</div>
<script>
data1.addEventListener("input",function() {
data_result.innerHTML = this.value.replace(/(?:\d)-/g,function($1) {
return $1.slice(0,1)+".";
});
}, false);
</script>
<div id="">
<span> :</span><br>
<input id="str_input">
<div id="all_str"></div>
</div>
<script>
str_input.addEventListener("input", function() {
all_str.innerHTML = this.value.replace(/[^a-zA-Z]/g,function($1) {
return "";
});
}, false);
</script>
<div>
<span> -:</span><br>
<input id="repeat">
<div id="repeat_div"></div>
</div>
<script>
repeat.addEventListener("input", function() {
repeat_div.innerHTML = this.value.replace(/(\w+)([\w]+)(\1)/gi,function($all,$1,$2,$3){
return $1+"_"+$3;
});
}, false);
/*
*
* {4,7} 4 ,
* {4,}
* {4}
* + === {1,}
* * === {0,}
* ? === {0,1} 0
*
* /
</script>
</body>
</html>