收集常用的Regex

來源:互聯網
上載者:User

匹配中文字元的Regex: [\u4e00-\u9fa5]

匹配雙位元組字元(包括漢字在內):[^\x00-\xff]

應用:計算字串的長度(一個雙位元組字元長度計2,ASCII字元計1)

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

匹配空行的Regex:\n[\s| ]*\r

匹配HTML標記的Regex:/<(.*)>.*<\/\1>|<(.*) \/>/

匹配首尾空格的Regex:(^\s*)|(\s*$)

應用:javascript中沒有像vbscript那樣的trim函數,我們就可以利用這個運算式來實現,如下:

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

利用Regex分解和轉換IP地址:

下面是利用Regex匹配IP地址,並將IP地址轉換成對應數值的Javascript程式:

function IP2V(ip)
{
re=/(\d+)\.(\d+)\.(\d+)\.(\d+)/g //匹配IP地址的Regex
if(re.test(ip))
{
return RegExp.$1*Math.pow(255,3))+RegExp.$2*Math.pow(255,2))+RegExp.$3*255+RegExp.$4*1
}
else
{
throw new Error("Not a valid IP address!")
}
}

不過上面的程式如果不用Regex,而直接用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地址的Regex:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

匹配網址URL的Regex:http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?

利用Regex去除字串中重複的字元的演算法程式:[註:此程式不正確,原因見本貼回複]

var s="abacabefgeeii"
var s1=s.replace(/(.).*\1/g,"$1")
var re=new RegExp("["+s1+"]","g")
var s2=s.replace(re,"")
alert(s1+s2) //結果為:abcefgi

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

用Regex限制只能輸入全形字元: onkeyup="value=value.replace(/[^\uFF00-\uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))"

用Regex限制只能輸入數字:onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"

用Regex限制只能輸入數字和英文:onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"

 

^\d+$  //匹配非負整數(正整數 + 0)
^[0-9]*[1-9][0-9]*$  //匹配正整數
^((-\d+)|(0+))$  //匹配非正整數(負整數 + 0)
^-[0-9]*[1-9][0-9]*$  //匹配負整數
^-?\d+$    //匹配整數
^\d+(\.\d+)?$  //匹配非負浮點數(正浮點數 + 0)
^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$  //匹配正浮點數
^((-\d+(\.\d+)?)|(0+(\.0+)?))$  //匹配非正浮點數(負浮點數 + 0)
^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$  //匹配負浮點數
^(-?\d+)(\.\d+)?$  //匹配浮點數
^[A-Za-z]+$  //匹配由26個英文字母組成的字串
^[A-Z]+$  //匹配由26個英文字母的大寫組成的字串
^[a-z]+$  //匹配由26個英文字母的小寫組成的字串
^[A-Za-z0-9]+$  //匹配由數字和26個英文字母組成的字串
^\w+$  //匹配由數字、26個英文字母或者底線組成的字串
^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$    //匹配email地址
^[a-zA-z]+://匹配(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$  //匹配url

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.