Match double-byte characters (including Chinese characters):
Application: Calculate the length of the string (one double-byte character length meter 2, ASCII character meter 1)
Regular expressions matching empty lines:
Regular expressions matching HTML tags:
Regular expressions matching the beginning and end spaces:
Application: There is no trim function like v bscript in j avascript, we can use this expression to implement it, as follows:
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
Decompose and convert IP addresses using regular expressions
The following is a Javascript program that uses regular expressions to match IP addresses and converts the IP addresses into corresponding values:
{
re=/(\d+)\.(\d+)\.(\d+)\.(\d+)/g // Regular expression matching IP address
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!")
}
}
However, if the above program does not use regular expressions, but uses the split function to decompose it directly. The program is as follows:
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 the email address:
Regular expression matching URL:
Algorithm program that uses regular expressions to remove duplicate characters in strings: [*Note: This program is incorrect]
var s1=s.replace(/(.).*\1/g,"$1")
var re=new RegExp("["+s1+"]","g")
var s2=s.replace(re,"")
alert(s1+s2) //The result is: abcefgi
*Note
===============================
If var s = "abacabefggeeii"
The result is wrong, the result is: abeicfgg
The ability of regular expressions is limited
===============================
I turned out to post on CSDN to find an expression to implement a way to remove duplicate characters, but I didn't find it, which is the easiest way to implement it that I can think of. The idea is to use backward reference to extract the characters that include duplicates, and then create a second expression with duplicate characters, and obtain unrepeated characters, and connect the two. This method may not work for strings that require character order.
You need to use regular expressions to extract file names from URL address, as shown in page1
s=s.replace(/(.*\/){ 0, }([^\.]+).*/ig,"$2")
alert(s)
Use regular expressions to restrict the input content in the text box in the web form:
Use regular expressions to restrict only Chinese:
Use regular expressions to restrict only full-width characters:
Use regular expressions to limit only numeric input:
Use regular expressions to restrict only numeric and English:
Match non-negative integers (positive integer + 0)
Match positive integers
Match non-positive integers (negative integer + 0)
Match negative integers
Match integers
Match non-negative floating point numbers (positive floating point numbers + 0)
Match positive floating point numbers
Match non-positive floating point numbers (negative floating point numbers + 0)
Match negative floating point numbers
Match floating point numbers
Match a string composed of 26 English letters
Match a string composed of 26 English letters capitalizations
Match a string consisting of 26 English letters lowercase
Match a string composed of numbers and 26 English letters
Match a string composed of numbers, 26 English letters or underscores
Match email address
Match URL
Match html tag
Visual Basic & C# Regular Expression
1. Confirm the valid email format
The following example uses a static method to verify that a string is in a valid email format. The IsValidEmail method returns true if the string contains a valid email address, otherwise it returns false but nothing else is taken. You can use IsValidEmail to filter out email addresses containing invalid characters before the application stores the address in a database or displays it in a page.
[Visual Basic]
' Return true if strIn is in valid e-mail format.
Return (strIn, ("^([\w-\.]+)@((\[[0-9]{ 1,3 }\.[0-9]{ 1,3 }\.[0-9]{ 1,3 }\.)|(([\w-]+\.)+))([a-zA-Z]{ 2,4 }|[0-9]{ 1,3 })(\]?)$")
End Function
[C#]
{
// Return true if strIn is in valid e-mail format.
return (strIn, @"^([\w-\.]+)@((\[[0-9]{ 1,3 }\.[0-9]{ 1,3 }\.[0-9]{ 1,3 }\.)|(([\w-]+\.)+))([a-zA-Z]{ 2,4 }|[0-9]{ 1,3 })(\]?)$");
}
2. Clean the input string
The following code example uses a static method to extract invalid characters from a string. You can use the CleanInput method defined here to clear out possible harmful characters entered in the text field of the form that accepts user input. CleanInput returns a string after clearing all non-alphanumeric characters except @, - (hyphen) and . (period).
[Visual Basic]
' Replace invalid characters with empty strings.
Return (strIn, "[^\w\.@-]", "")
End Function
[C#]
{
// Replace invalid characters with empty strings.
return (strIn, @"[^\w\.@-]", "");
}
3. Change date format
The following code example uses the method to replace the date form of mm/dd/yy with the date form of dd-mm-yy.
[Visual Basic]
Return (input, _
"\b(?<month>\d{ 1,2 })/(?<day>\d{ 1,2 })/(?<year>\d{ 2,4 })\b", _
"${ day }-${ month }-${ year }")
End Function
[C#]
{
return (input,"\\b(?<month>\\d{ 1,2 })/(?<day>\\d{ 1,2 })/(?<year>\\d{ 2,4 })\\b","${ day }-${ month }-${ year }");
}
Regex replacement mode
This example shows how to use named backreferences in the replacement mode of . Where, the replacement expression ${ day } inserts the substring captured by the (?…) group.
There are several static functions that allow you to use regular expression operations without creating explicit regular expression objects, and functions are one of them. This will give you convenience if you don't want to keep compiled regexes
4. Extract URL information
The following code example uses to extract protocols and port numbers from URLs. For example, ":8080... will return "http:8080".
[Visual Basic]
Dim r As New Regex("^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/", _
)
Return (url).Result("${ proto }${ port }")
End Function
[C#]
{
Regex r = new Regex(@"^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/",
);
return (url).Result("${ proto }${ port }");
}
Regular expressions for passwords that only letters and numbers are not less than 6 digits, and both letters of numerical letters contain
In C#, this can be used to represent:
An algorithm program that requires splitting the path string into two parts: root directory and subdirectory. Considering the path formats are: C:\aa\bb\cc , \\aa\bb\cc , ftp:///cc The above paths will be split into: C:\ and aa\bb\cc , \\aa and \bb\cc , ftp:// and /cc are implemented as follows with javascript:
var regPathParse=/^([^\\^\/]+[\\\/]+|\\\\[^\\]+)(.*)$/
if(regPathParse.test(strFolder))
{
strRoot=RegExp.$1
strSub=RegExp.$2
}