Javascript判断空格

[ 2004-09-09 00:41:16 | Author: zhenhua ]
Font Size: Large | Medium | Small
正则
<script>
 function checkvalue(){
 if(document.Form.content.value.replace(/\s/g,"")=="")

 {
 alert("内容不能为空!");
 document.addForm.content.focus();
 return false;
 }
 document.addForm.submit();
 return true;
}
</script>
 

匹配中文字符的正则表达式: [\u4e00-\u9fa5]

匹配双字节字符(包括汉字在内): [^\x00-\xff]

应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)

String.prototype.len=function(){return this.replace([^\x00-\xff]/g,"aa").length;}

匹配空行的正则表达式: \n[\s| ]*\r

匹配HTML标记的正则表达式: /<(.*)>.*<\/>|<(.*) \/>/

匹配首尾空格的正则表达式: (^\s*)|(\s*$)

应用:javascript中没有像vbscript那样的trim函数,我们就可以利用这个表达式来实现,如下:

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

利用正则表达式分解和转换IP地址:

下面是利用正则表达式匹配IP地址,并将IP地址转换成对应数值的javascript程序:
function IP2V(ip) {
re=/(\d+)\.(\d+)\.(\d+)\.(\d+)/g //匹配IP地址的正则表达式
if(re.test(ip)) {
return RegExp.*Math.pow(255,3))+RegExp.*Math.pow(255,2))+RegExp.*255+RegExp.*1
}
else {
throw new Error("Not a valid IP address!")
}
}

不过上面的程序如果不用正则表达式,而直接用split函数来分解可能更简单,程序如下:
var ip="10.100.20.168"
ip=ip.split(".")
alert("IP值是:"+(ip[0]*255*255*255+ip[1]*255*255+ip[2]*255+ip[3]*1))

匹配Email地址的正则表达式: \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

匹配网址URL的正则表达式: http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?

利用正则表达式去除字串中重复的字符的算法程序:

var s="abacabefgeeii"
var s1=s.replace(/(.).*/g,"")
var re=new RegExp("["+s1+"]","g")
var s2=s.replace(re,"")
alert(s1+s2) //结果为:abcefgi

用正则表达式从URL地址中提取文件名的javascript程序,如下结果为page1

s="http://www.9499.net/page1.htm"
s=s.replace(/(.*\/)([^\.]+).*/ig,"")
alert(s)

利用正则表达式限制网页表单里的文本框输入内容:

用正则表达式限制只能输入中文: onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))"

用正则表达式限制只能输入全角字符: onkeyup="value=value.replace(/[^\uFF00-\uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))"

用正则表达式限制只能输入数字: onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"

用正则表达式限制只能输入数字和英文: onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"
Comments Feed Comments Feed: http://www.zhenhua.org/feed.asp?q=comment&id=161
UTF-8 Encoding Trackback URL: http://www.zhenhua.org/trackback.asp?id=161

View Mode: Show All | Comments: 0 | Trackbacks: 363 | Toggle Order | Views: 28948
Trackback fioricet dose
[ fioricet dose | 2012-11-21 02:32:38 ]
Brand and Generic Drugs, Discount Pharmacy. All information about fioricet dose in one place: http://rush.de.ki/fioricet/fioricet-dose.html
Trackback anchorage butalbital
[ anchorage butalbital | 2012-11-28 02:49:45 ]
Brand and Generic Drugs, Discount Pharmacy. All information about anchorage butalbital in one place: http://digilander.libero.it/michele_15/butalbital/anchorage-butalbital.html
Trackback verapamil overdose
[ verapamil overdose | 2012-12-20 21:08:45 ]
Brand and Generic Drugs, Discount Pharmacy. All information about verapamil overdose in one place: http://mitglied.multimania.de/haenel/verapamil/verapamil-overdose.html
Trackback canadian chemist
[ canadian chemist | 2013-01-10 06:03:22 ]
Brand and Generic Drugs, Discount Pharmacy. All information about canadian chemist in one place: http://perso.wanadoo.es/stukov22/canadian-pharmacy/canadian-chemist.html
Trackback morphium
[ morphium | 2013-01-26 02:52:21 ]
Brand and Generic Drugs, Discount Pharmacy. All information about morphium in one place: http://m-d-x.de-info.de/morphine/morphium.html
Trackback memphis sibutramine
[ memphis sibutramine | 2013-02-16 08:11:34 ]
Brand and Generic Drugs, Discount Pharmacy. All information about memphis sibutramine in one place: http://hem.passagen.se/a.alex/sibutramine/memphis-sibutramine.html
Trackback medical symptoms
[ medical symptoms | 2013-02-21 03:47:47 ]
Brand and Generic Drugs, Discount Pharmacy. All information about medical symptoms in one place: http://braco.a.gp/mobic/medical-symptoms.html
Trackback provigil and birth control
[ provigil and birth control | 2013-03-01 09:43:57 ]
Brand and Generic Drugs, Discount Pharmacy. All information about provigil and birth control in one place: http://perso.wanadoo.es/turadio/provigil/provigil-and-birth-control.html
Trackback order ephedrine hcl powder
[ order ephedrine hcl powder | 2013-03-20 08:52:10 ]
Brand and Generic Drugs, Discount Pharmacy. All information about order ephedrine hcl powder in one place: http://lamed.a.gp/ephedrine/order-ephedrine-hcl-powder.html
Trackback flomax mexico
[ flomax mexico | 2013-05-30 03:25:38 ]
Brand and Generic Drugs, Discount Pharmacy. All information about flomax mexico in one place: http://mitglied.multimania.de/cruwel/flomax/flomax-mexico.html
Trackback get modafinil in canada
[ get modafinil in canada | 2013-06-09 22:11:50 ]
Brand and Generic Drugs, Discount Pharmacy. All information about get modafinil in canada in one place: http://digilander.libero.it/provagdr/modafinil/get-modafinil-in-canada.html
Trackback foreign pharmacy degree
[ foreign pharmacy degree | 2013-06-13 18:37:30 ]
Brand and Generic Drugs, Discount Pharmacy. All information about foreign pharmacy degree in one place: http://hem.passagen.se/supern0va/foreign-pharmacy/foreign-pharmacy-degree.html
Trackback clarksville geodon
[ clarksville geodon | 2013-07-19 00:57:50 ]
Brand and Generic Drugs, Discount Pharmacy. All information about clarksville geodon in one place: http://perso.wanadoo.es/septiembre112006/geodon/clarksville-geodon.html
Trackback online pharmacies
[ online pharmacies | 2013-08-23 04:39:26 ]
Brand and Generic Drugs, Discount Pharmacy. All information about online pharmacies in one place: http://fron.altervista.org/tramadol/online-pharmacies.html
Trackback eugene bactroban
[ eugene bactroban | 2013-08-25 08:21:01 ]
Brand and Generic Drugs, Discount Pharmacy. All information about eugene bactroban in one place: http://calc.de-info.de/bactroban/eugene-bactroban.html
Trackback order mexico
[ order mexico | 2013-09-11 02:05:40 ]
Brand and Generic Drugs, Discount Pharmacy. All information about order mexico in one place: http://digilander.libero.it/denisspolja/viagra/order-mexico.html
Trackback get prednisone out your body
[ get prednisone out your body | 2013-09-22 18:32:37 ]
Brand and Generic Drugs, Discount Pharmacy. All information about get prednisone out your body in one place: http://kotler.snn.gr/prednisone/get-prednisone-out-your-body.html
Trackback vardenafil dosage
[ vardenafil dosage | 2013-10-09 07:48:59 ]
Brand and Generic Drugs, Discount Pharmacy. All information about vardenafil dosage in one place: http://digilander.libero.it/provaferrini/vardenafil/vardenafil-dosage.html
Trackback i need cheap alprazolam
[ i need cheap alprazolam | 2013-10-11 07:59:01 ]
Brand and Generic Drugs, Discount Pharmacy. All information about i need cheap alprazolam in one place: http://hem.passagen.se/mariasg/alprazolam/i-need-cheap-alprazolam.html
Trackback canadian pharmacy coupon
[ canadian pharmacy coupon | 2013-11-02 20:46:06 ]
Brand and Generic Drugs, Discount Pharmacy. All information about canadian pharmacy coupon in one place: http://povinko.host.sk/pharmacy/canadian-pharmacy/canadian-pharmacy-coupon.html

If you feel this site you find this information helpful, please click on the donation, which is voluntary,Thank you.
Post Comment
Smilies
[arrow] [biggrin] [confused] [cool]
[cry] [eek] [evil] [exclaim]
[frown] [idea] [lol] [mad]
[mrgreen] [neutral] [question] [razz]
[redface] [rolleyes] [sad] [smile]
[surprised] [twisted] [wink]
Enable UBB Codes
Auto Convert URL
Show Smilies
Hidden Comment
Username:   Password:   Register Now?
Security Code * Please Enter the Security Code