js经常使用正则表达式实例

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

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

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

String.prototype.len=function()ajax

{正则表达式

return this.replace([^\x00-\xff]/g,"aa").length;ide

}函数

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

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

应用:javascript中没有像Vbscript那样的trim函数,咱们能够利用这个表达式来实现,如:prototype

String.prototype.trim=function ()

{

return this.replace(/(^\s*)|(\s*$)/g,"");

}

 

摘自:http://ajax.cnrui.cn/article/2/2/2006/20060411016.shtml

相关文章
相关标签/搜索