有时候会要验证自己写的正则表达式是否正确
所以写了这个小东西:
demo:js正则表达式验证
html:
<h3>绿色表示匹配,红色表示不匹配</h3>
<label>请在下框填入正则表达式</label><br /> <textarea id="regexp"></textarea><br /> <label>请在下框填需要验证的式子</label><br /> <textarea id="needtest"></textarea>
css:
#regexp,#needtest { height:100px; width:300px; resize:none; font-weight:bold;}
javascript:
window.onload = function(){ document.getElementById('needtest').onkeyup = function(){
var ret = document.getElementById('regexp').value;
ret = '^' + ret + '$';
var re = new RegExp(ret) if(re.test(document.getElementById('needtest').value)){ document.getElementById('needtest').style.color = 'green';
}else{
document.getElementById('needtest').style.color = 'red';
}
} document.getElementById('regexp').onkeyup = function(){
var ret = document.getElementById('regexp').value;
ret = '^' + ret + '$';
var re = new RegExp(ret) if(re.test(document.getElementById('needtest').value)){ document.getElementById('needtest').style.color = 'green';
}else{
document.getElementById('needtest').style.color = 'red';
}
} }