Home > Web Front-end > JS Tutorial > body text

Several examples of regular expressions in javascript

伊谢尔伦
Release: 2016-11-22 14:26:35
Original
1077 people have browsed it

! 去除字符串两端空格的处理

如果采用传统的方式,就要可能就要采用下面的方式了

//清除左边空格
function js_ltrim(deststr)
{
if(deststr==null)return "";
var pos=0;
var retStr=new String(deststr);
if (retStr.lenght==0) return retStr;
while (retStr.substring(pos,pos+1)==" ") pos++;
retStr=retStr.substring(pos);
return(retStr);
}
//清除右边空格
function js_rtrim(deststr)
{
if(deststr==null)return "";
var retStr=new String(deststr);
var pos=retStr.length;
if (pos==0) return retStr;
while (pos && retStr.substring(pos-1,pos)==" " ) pos--;
retStr=retStr.substring(0,pos);
return(retStr);
}
//清除左边和右边空格
function js_trim(deststr)
{
if(deststr==null)return "";
var retStr=new String(deststr);
var pos=retStr.length;
if (pos==0) return retStr;
retStr=js_ltrim(retStr);
retStr=js_rtrim(retStr);
return retStr;
}
Copy after login

采用正则表达式,来去除两边的空格,只需以下代码

String.prototype.trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
Copy after login

一句就搞定了,
可见正则表达式为我们节省了相当的编写代码量

! 移动手机号的校验

如果采用传统的校验方式至少就要完成下面三步的校验,
(1). 是否是数字
(2).是否是11位
(3).数字的第三位是否是5,6,7,8,9
如果采用正则表达式校验,只需以下代码

function checkMobile1(form)
{
if (form.mobile.value > "")
{
var reg=/13[5,6,7,8,9]\d{8}/;
if ( form.mobile.value.match(reg)== null)
{
alert("请输入正确的移动手机号码!");
form.mobile.focus(); return false;
}
}
return true;
}
Copy after login

从上面的代码可以看出校验移动手机号只需定义一个var reg=/13[5,6,7,8,9]\d{8}/;模式匹配串就可以完成合法性校验了

! URL的校验,
条件:必须以http:// 或 https:// 开头, 端口号必须为在1-65535 之间, 以下代码完成了合法性校验

//obj:数据对象
//dispStr :失败提示内容显示字符串
function checkUrlValid( obj, dispStr)
{
if(obj == null)
{
alert("传入对象为空");
return false;
}
var str = obj.value;
var urlpatern0 = /^https?:\/\/.+$/i;
if(!urlpatern0.test(str))
{
alert(dispStr+"不合法:必须以'http:\/\/'或'https:\/\/'开头!");
obj.focus();
return false;
}
var urlpatern2= /^https?:\/\/(([a-zA-Z0-9_-])+(\.)?)*(:\d+)?.+$/i;
if(!urlpatern2.test(str))
{
alert(dispStr+"端口号必须为数字且应在1-65535之间!");
obj.focus();
return false;
}
var urlpatern1 =/^https?:\/\/(([a-zA-Z0-9_-])+(\.)?)*(:\d+)?(\/((\.)?(\?)?=?&?[a-zA-Z0-9_-](\?)?)*)*$/i;
if(!urlpatern1.test(str))
{
alert(dispStr+"不合法,请检查!");
obj.focus();
return false;
}
var s = "0";
var t =0;
var re = new RegExp(":\\d+","ig");
while((arr = re.exec(str))!=null)
{
s = str.substring(RegExp.index+1,RegExp.lastIndex);
if(s.substring(0,1)=="0")
{
alert(dispStr+"端口号不能以0开头!");
obj.focus();
return false;
}
t = parseInt(s);
if(t<1 || t >65535)
{
alert(dispStr+"端口号必须为数字且应在1-65535之间!");
obj.focus();
return false;
}
}
return true;
}
Copy after login

对url的校验,看上去有很多的代码,这是因为要给予出错提示, 否则只需var urlpatern1 =/^https?:\/\/(([a-zA-Z0-9_-])+(\.)?)*(:\d+)?(\/((\.)?(\?)?=?&?[a-zA-Z0-9_-](\?)?)*)*$/i; 一句就可以校验出url合法性了


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!