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

相關文章
相關標籤/搜索