Home >Backend Development >PHP Tutorial >Regular expression java PHP some conversions of some commonly used regular expression characters

Regular expression java PHP some conversions of some commonly used regular expression characters

WBOY
WBOYOriginal
2016-07-29 08:38:331245browse

Match double-byte characters (including Chinese characters): [^x00-xff]
Application: Calculate the length of a string (the length of a double-byte character counts as 2, and the ASCII character counts as 1)
String.prototype.len=function( ){return this.replace([^x00-xff]/g,"aa").length;}
Regular expression to match blank lines: n[s| ]*r
Regular expression to match HTML tags: / <(.*)>.*|<(.*) />/
Regular expression matching leading and trailing spaces: (^s*)|(s*$)
Application: javascript There is no trim function like vbscript, we can use this expression to achieve it, as follows:
String.prototype.trim = function() {
return this.replace(/(^s*)|(s*$) /g, "");
}
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 //Regular expression matching IP address
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!")
}
}
 However, if the above program does not use regular expressions, it may be simpler to use the split function to decompose it directly. The program is as follows:
var ip="10.100.20.168"
ip=ip.split(".")
alert(" The IP value is: "+(ip[0]*255*255*255+ip[1]*255*255+ip[2]*255+ip[3]*1))
  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) //The result is: abcefgi
A javascript program that uses regular expressions to extract file names from URL addresses. The following result is page1
s="http://www.9499.net/page1.htm"
s=s.replace(/(.*/)([^.]+).*/ig,"")
alert(s)
Use regular expressions to limit the input content of the text box in the web form:
Use The regular expression restricts the input of only Chinese characters:
/[^u4E00-u9FA5]/g,'')" /[^u4E00-u9FA5]/g,''))"
The regular expression restricts the input of only full-width characters. :
/[^uFF00-uFFFF]/g,'')" /[^uFF00-uFFFF]/g,''))"
Use regular expressions to limit the input of numbers only:
/[^d]/g ,'') "/[^d]/g,''))"
Use regular expressions to limit the input to numbers and English only:
/[W]/g,'') "/[^d]/g ,''))

The above introduces some conversions of some commonly used regular expression characters in regular expression java PHP, including the content of regular expression java. I hope it will be helpful to friends who are interested in PHP tutorials.

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