去掉字符串头尾多余的空格
/g是全文查找所有匹配function string.prototype.trim(){return this.replace(/(^\s*)|(\s*$)/g, "");}function string.prototype.ltrim(){return this.replace(/(^\s*)/g, "");}
function String.prototype.RTrim(){return this.replace(/(\s*$)/g, "");}
--------------------------------------------------------------
应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)
String.prototype.len=function(){return this.replace([^\x00-\xff]/g,"aa").length;}
--------------------------------------------------------------
应用:javascript中没有像vbscript那样的trim函数,我们就可以利用这个表达式来实现,如下:
立即学习“Java免费学习笔记(深入)”;
String.prototype.trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
得用正则表达式从URL地址中提取文件名的javascript程序,如下结果为page1
s="http://www.9499.net/page1.htm"
s=s.replace(/(.*\/){0,}([^\.]+).*/ig,"")
alert(s)
1 系统使用三层构架2 数据库访问使用sqlHelper3 编辑器使用FreeTextBox4 布局采用Div+Css5 正则表达式实现数据验证6 动态构建sql查询语句
##利用正则表达式限制网页表单里的文本框输入内容:
--------------------------------------------------------------
用正则表达式限制只能输入中文: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,'))"










