Detailed explanation of commonly used regular expression character conversion in PHP

伊谢尔伦
Release: 2023-03-11 22:50:02
Original
1254 people have browsed it

For some front-end regular operations, you can modify it to become PHP's

to match double-byte characters (including Chinese characters): [^\x00-\xff]

Application: Calculate the length of string (the length of a double-byte character counts as 2, and the length of an ASCII character counts as 1)

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

Matching the regular expression# for empty lines ##: \n[\s| ]*\r
Regular expression matching HTML tags: /<(.*)>.*<\/>|<(.* ) \/>/

Regular expression matching leading and trailing spaces: (^\s*)|(\s*$)

Application: Not available in
javascript For a trim function like vbscript, we can use this expression to implement it, as follows:

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

Use regular expressions to decompose and convert IP addresses:


The following is a javascript program that uses regular expressions to match IP addresses and convert IP addresses into corresponding values:

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!")  
} 
}
Copy after login

However, if the above program does not use regular expressions, but directly uses the split function to decompose it It may be simpler, the program is as follows:


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))
Copy after login

Regular expression matching email address: \w+([-+.]\w+)*@\w+([-.]\w+)*\ .\w+([-.]\w+)*


Regular expression matching URL: http://([\w-]+\.)+[\w-]+(/[ \w- ./?%&=]*)?

Algorithm program for removing repeated characters from a string using regular expressions:

var s="abacabefgeeii" 
var s1=s.replace(/(.).*/g,"")  
var re=new RegExp("["+s1+"]","g")  
var s2=s.replace(re,"")  
alert(s1+s2) //结果为:abcefgi
Copy after login

Using regular expressions to extract characters from a URL A javascript program that extracts the file name from the address. The following result is page1


s="http://www.php.cn/page1.htm"  
s=s.replace(/(.*\/)([^\.]+).*/ig,"")  
alert(s)
Copy after login
Use regular expressions to limit the input content of the text box in the web form:


Use regular expressions to limit only Enter Chinese:


onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text' ,clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))"
Use regular expressions to limit the input of only full-width characters:

onkeyup="value=value.replace(/[^\uFF00-\uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/ [^\uFF00-\uFFFF]/g,''))"

Use regular expressions to limit input to numbers:

onkeyup="value=value.replace(/[^ \d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"

Use regular expressions to limit input to numbers and English only:

onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text ',clipboardData.getData('text').replace(/[^\d]/g,''))

The above is the detailed content of Detailed explanation of commonly used regular expression character conversion in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template