正则表达式的匹配
标签
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
*{margin:0;padding:0;}
textarea{width:500px;height:100px;margin-top: 30px;border:1px solid #f00;}
</style>
<script type="text/javascript">
window.onload=function(){
var oA1 = document.getElementById("a1");
var oA2 = document.getElementById("a2");
var oBtn = document.getElementById("btn");
var re = /<[^>]+>/g;
//var re = /<[\w\W]+>/g;
oBtn.onclick = function(){
oA2.value = oA1.value.replace(re,'');
}
}
</script>
</head>
<body>
<input type="button" value="点击" id="btn"> <br />
<textarea id="a1">
</textarea>
<textarea id="a2">
</textarea>
</body>
</html>
匹配13-99之间年龄
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript">
//var arr = "fd5g456fg465dfg45dfg231dfg45gf2";
var arr = [6,11,23,19,108,13,26,12,11,95,16,6,56,42,9,45,99];
function findNum(arr){
//return arr.match(/[13-99]+/g);
var newArr = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i]>=13 && arr[i]<=99) {
newArr.push(arr[i]);
};
};
return newArr;
}
alert(findNum(arr));
</script>
</head>
<body>
</body>
</html>
去掉大写字母
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>去掉大写字母</title>
<script>
var str = "CfdDS2645F6sfJ54K4PPPdOsAf"
function test(str){
var re = /[A-Z]+/g;
return str.replace(re,"");
}
alert(test(str));
</script>
</head>
<body>
</body>
</html>
去掉数字
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>去掉数字</title>
<script type="text/javascript">
var str = "df56gh12gfhDFG312B9H74G5F5Hd21fg12";
function test(str){
var re = /\d+/g;
return str.replace(re,"");
}
alert(test(str));
</script>
</head>
<body>
</body>
</html>
手机号码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>手机号码</title>
<script>
window.onload=function(){
var oTxt = document.getElementById("txt");
var oBtn = document.getElementById("btn");
oBtn.onclick = function(){
var re = /^1[0-9]{10}$/;
var oV = oTxt.value;
if (re.test(oV)) {
document.getElementsByTagName('span')[0].style.display = "block";
document.getElementsByTagName('span')[1].style.display = "none";
}
else{
document.getElementsByTagName('span')[1].style.display = "block";
document.getElementsByTagName('span')[0].style.display = "none";
}
}
}
</script>
</head>
<body>
<h1>请输入电话号码</h1>
<input type="text" id="txt" />
<input type="button" value="验证" id="btn" />
<span style="display:none;">验证通过</span>
<span style="display:none;">你的输入有误</span>
</body>
</html>