正则
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>正则</title> </head> <body> <input type="text" id="regexp"> <script> var $ = function(id){ return document.getElementById(id); } var addEvent = function(obj,event,fn){ if(obj.addEventListener){ obj.addEventListener(event,fn,false); }else if(obj.attachEvent){ obj.attachEvent("on"+event,fn); } } var regexp = $("regexp"); //test方法 /*var regexp = $("regexp"); addEvent(regexp,'change',function(){ //var pt1 = /^\d+$/g; var pt1 = new RegExp('^\\d+$'); if(pt1.test(regexp.value)){ console.log(1); }else{ console.log(0); } })*/ //replace方法 /*addEvent(regexp,'keyup',function(){ var pt2 = /\s/; regexp.value = regexp.value.replace(pt2,'0'); })*/ //分支写法 /*addEvent(regexp,'change',function(){ var pt3 = /^([1-9]\d*(\.\d{2})?|0(\.\d{1,2})?)$/g; if(pt3.test(regexp.value)){ console.log(1); }else{ console.log(0); } })*/ //零宽断言 /*addEvent(regexp,'change',function(){ var pt4 = /\b\w+(?=ing\b)/g;//零宽度正预测先行断言 var pt5 = /(?<=\bre)\w+\b/g;//零宽度正回顾后发断言,js不支持 regexp.value = regexp.value.replace(pt5,'ccc'); })*/ </script> </body> </html>