//周末在家复习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>