1.邮箱检测正则
function isEmail(_sMail){
_sMail = _sMail || "";
var sRegValue= "^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((/.[a-zA-z0-9_-]{2,3}){1,2})$";
var emailReg = new RegExp(sRegValue,"ig");
return emailReg.test(_sMail);
}
var sMyMail = "[email protected]";
alert(isEmail(sMyMail)); //打印 true
2.验证是否是url地址
function isUrl(_sUrl){
_sUrl = _sUrl || "";
var sRegValue = "^(http|https):////.";
var urlReg = new RegExp(sRegValue,"ig");
return urlReg.test(_sUrl);
}
alert(isUrl("http://www.sina.com")); // true
3.验证指定的字符串是否是style标签
function testStyleHtml(){
var sHtml = "<style>.class1{background-color:red;font-size:12px;}</style>";
var styleReg = new RegExp("(?:<style.*?>)(([.|/n])*?)(?:<//style>)","ig"); //"(?:pattern)"表示pattern非获取匹配;“?”跟在“*,+,?,{n},{n,},{n,m}”后边表示非贪婪匹配
var sResult = styleReg.test(sHtml);
alert(sResult);// true
}
4.检查一段字符串是否含有html标签
function isHtmlTag(){
var sHtml = "<style>.class1{background-color:red;font-size:12px;}</style>";
var tagReg = new RegExp("<(.*)>.*<///1>","ig"); //为什么这个不可以??,这里打出false
tagReg = /<(.*)>.*<///1>/ig;//这种正则的定义方式可以,这个正则打印出true
alert(tagReg.test(sHtml));
}