Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the regular expressions commonly used in the Internet

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the regular expressions commonly used in the Internet, the article is very detailed, has a certain reference value, interested friends must read it!

Regular expressions that match Chinese characters: [\ u4e00 -\ u9fa5]

Match double-byte characters (including Chinese characters): [^\ X00 -\ xff]

Application: calculates the length of a string (a double-byte character length meter 2 ~ ASCII character counter 1)

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

Regular expression that matches blank lines:\ n [\ s |] *\ r

Regular expression that matches the HTML tag: /. * | /

Regular expression that matches leading and trailing spaces: (^\ s*) | (\ sblank $)

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

Use regular expressions to decompose and translate IP addresses:

The following is a Javascript program that uses regular expressions to match IP addresses and converts IP addresses to corresponding values:

Function IP2V (ip) {re=/ (\ d+)\. (\ d+) / g / / regular expression if (re.test (ip)) {return RegExp.1 ∗ Math.pow (255) + RegExp.2*Math.pow (255) + 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 directly use the split function to decompose it may be easier, the program is as follows:

Var ip= "10.100.20.168" ip=ip.split (".") alert ("IP value is:" + (ip [0] * 255*255*255+ip [1] * 255*255+ip [2] * 255+ip [3] * 1))

The regular expression that matches the Email address:\ W + ([- +.]\ w +) * @\ w + ([-.]\ w +) *.\ w + ([-.]\ w +) *

The regular expression that matches the URL URL: http://([\w-]+\.)+[\w-]+(/[\w-. /?% & =] *?

An algorithmic program that uses regular expressions to remove repeated characters from a string: [note: this program is incorrect, see red font]

Var s = "abacabefgeeii" var s1=s.replace (/ (.). *\ 1CMG, "$1") var re=new RegExp ("[" + S1 + "]", "g") var s2=s.replace (re, ") alert (s1+s2) / / the result is: abcefgi

= =

If var s = "abacabefggeeii"

The result is wrong, the result is: abeicfgg

The ability of regular expressions is limited

I originally posted on CSDN looking for an expression to remove repetitive characters, but I didn't find it. This is the simplest implementation I can think of. The idea is to use the backward reference to take out the characters including repetition, then create a second expression with the repeated characters, take the non-repetitive characters, and concatenate the two. This method may not apply to strings that require character order.

The javascript program that extracts the file name from the URL address with a regular expression, the following result is page1

S = "http://www.9499.net/page1.htm"s=s.replace(/(.*\/){0,}([^\.]+).*/ig,"$2")alert(s)

Use regular expressions to restrict text box input in a web form:

Use regular expressions to restrict input to Chinese only:

Onkeyup= "value=value.replace (/ [^\ u4E00 -\ u9FA5] / g text'')" onbeforepaste=clipboardData.setData ('text',clipboardData.getData (' text'). Replace (/ [^\ u4E00 -\ u9FA5] / g charge')) "

Use regular expressions to restrict the input of full-width characters:

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

Use regular expressions to limit input to numbers:

Onkeyup= "value=value.replace (/ [^\ d] / gjinger')" onbeforepaste= "clipboardData.setData ('text',clipboardData.getData (' text'). Replace (/ / [^\ d] / gjime'))"

Use regular expressions to limit input to numbers and English:

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

Application: there is no trim function like vbscript in javascript, so we can use this expression to implement it, as follows:

/ * calculates the string length and calculates non-wide characters as half a character * * @ param str * @ returns * / function gbLength (str) {return typeof str = "string"? Str.length + str.replace (/ [\ x00 -\ x7f] / g,'). Length + 1 > > 1: 0;} / * gets the substring of the specified length of the string and calculates the non-wide character as half a character * * @ param str * @ param length * @ returns * / function gbSubstr (str, length) {if (typeof str! = "string") return str; length + = length; for (var n = 0, L = str.length; n)

< L && length >

0; return str.substr +) {length-= str.charCodeAt (n) > 1272: 1;} return str.substr (0, n);} / * cut the specified length substring of the specified string. If the length exceeds the specified length, discard the last two characters and append. * * @ param str * @ param len * @ returns * / function fit_length (str, len) {len = len | | 16 If (typeof str! = = "string" | | str.length

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report