search
Homephp教程PHP源码动态网页制作PHP常用的正则表达式

<script>ec(2);</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.etoow.com/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,'')

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools