Use of regular expressions in JavaScript and common regular expressions

1. Use of regular expressions A regular expression can be regarded as the characteristic description of a character segm...

1. Use of regular expressions

A regular expression can be regarded as the characteristic description of a character segment, and its function is to find the substring satisfying the conditions from a pile of strings.
For example, define a regular expression in JavaScript

var reg = /hello/ or var reg = new RegExp("hello")

Then this regular expression can be used to find the word hello from a pile of strings.
The action of "find" may result in finding the location of the first hello, replacing hello with another string, finding all Hello, etc.
Here are some functions that can use regular expressions in JavaScript

String.prototype.search method
It is used to find the index of the first occurrence of a substring in the original string. If not, it returns - 1

"abchello".search(/hello/); // 3

String.prototype.replace method
Used to replace substrings in a string

"abchello".replace(/hello/,"hi"); // "abchi"

String.prototype.split method
Used to split strings

"abchelloasdasdhelloasd".split(/hello/); //["abc", "asdasd", "asd"]

String.prototype.match method
Used to capture substrings in a string into an array;
By default, only one result is captured in the array. When the regular expression has the attribute of "global capture" (parameter g), all results will be captured in the array

"abchelloasdasdhelloasd".match(/hello/); //["hello"] "abchelloasdasdhelloasd".match(/hello/g); //["hello","hello"]

RegExp.prototype.test method
Used to test whether the string contains substrings

/hello/.test("abchello"); // true

RegExp.prototype.exec method
Similar to the string match method, this method also captures the qualified string from the string to the array, but there are two differences.

  1. The exec method can only capture a part of the string into the array at a time, regardless of whether the regular expression has global attributes or not;
var reg=/hello/g; reg.exec("abchelloasdasdhelloasd"); // ["hello"]
  1. The regular expression object (RegExp) has a lastIndex property that indicates where to start capturing next. After each exec method is executed, lastIndex will push back until no matching character is found and null is returned, and then capture from scratch. This property can be used to traverse the substring in the captured string.
var reg = /hello/g; reg.lastIndex; //0 reg.exec("abchelloasdasdhelloasd"); // ["hello"] reg.lastIndex; //8 reg.exec("abchelloasdasdhelloasd"); // ["hello"] reg.lastIndex; //19 reg.exec("abchelloasdasdhelloasd"); // null reg.lastIndex; //0

2. Common regular expressions

2.1 expression of check number
Number:^[0-9]*$ n Digits:^\d$ at least n Digits:^\d$ m-n Digits:^\d$ Numbers starting with zero and non-zero:^(0|[1-9][0-9]*)$ Non zero digits with up to two decimal places:^([1-9][0-9]*)+(.[0-9])?$ Band 1-2 Positive or negative number of decimal places:^(\-)?\d+(\.\d)?$ Positive, negative, and decimal:^(\-|\+)?\d+(\.\d+)?$ Positive real number with two decimal places:^[0-9]+(.[0-9])?$ Have 1~3 Positive real number with decimal places:^[0-9]+(.[0-9])?$ Non zero positive integer:^[1-9]\d*$ or ^([1-9][0-9]*)$ or ^\+?[1-9][0-9]*$ Nonzero negative integer:^\-[1-9][]0-9"*$ or ^-[1-9]\d*$ Nonnegative integer:^\d+$ or ^[1-9]\d*|0$ Non positive integer:^-[1-9]\d*|0$ or ^((-\d+)|(0+))$ Nonnegative floating point number:^\d+(\.\d+)?$ or ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$ Non positive floating point number:^((-\d+(\.\d+)?)|(0+(\.0+)?))$ or ^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$ Positive floating point number:^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ or ^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$ Negative floating point number:^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ or ^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$ Floating point number:^(-?\d+)(\.\d+)?$ or ^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$
2.2 expression of check character
chinese characters:^[\u4e00-\u9fa5]$ English and figures:^[A-Za-z0-9]+$ or ^[A-Za-z0-9]$ Length 3-20 All characters of:^.$ A string of 26 English letters:^[A-Za-z]+$ A string of 26 uppercase English letters:^[A-Z]+$ A string of 26 lowercase English letters:^[a-z]+$ A string consisting of numbers and 26 English letters:^[A-Za-z0-9]+$ A string consisting of numbers, 26 English letters, or underscores:^\w+$ or ^\w$ Chinese, English and figures include underline:^[\u4E00-\u9FA5A-Za-z0-9_]+$ Chinese, English, numbers but excluding underscores and other symbols:^[\u4E00-\u9FA5A-Za-z0-9]+$ or ^[\u4E00-\u9FA5A-Za-z0-9]$ You can enter information containing^%&',;=?$\"Equal characters:[^%&',;=?$\x22]+ Prohibit input with~Characters:[^~\x22]+
2.3 expression of special requirements
Email Address:^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$ Domain name:[a-zA-Z0-9][-a-zA-Z0-9](/.[a-zA-Z0-9][-a-zA-Z0-9])+/.? InternetURL: [a-zA-z]+://[^ \ s] * or ^ http: / / ([\ W -] + \.) + [\ W -] + (/ [\ W -. /?% & =] *)$ phone number:^(13[0-9]|14[0-9]|15[0-9]|16[0-9]|17[0-9]|18[0-9]|19[0-9])\d$ (Since the number issuing section of the Ministry of industry and information technology is irregular, it is recommended to use pan parsing ^([1][3,4,5,6,7,8,9])\d$) Telephone number("XXX-XXXXXXX","XXXX-XXXXXXXX","XXX-XXXXXXX","XXX-XXXXXXXX","XXXXXXX"and"XXXXXXXX): ^(\(\d-)|\d-)?\d$ Domestic telephone number(0511-4405222,021-87888822): \d-\d|\d-\d 18 Bit ID number(Numbers, letters x ending): ^((\d)|([0-9x])|([0-9X]))$ Is the account number legal(Starting with a letter, 5 is allowed-16 Bytes, alphanumeric underscores allowed): ^[a-zA-Z][a-zA-Z0-9_]$ password(Starts with a letter and is 6 in length~18 Can only contain letters, numbers, and underscores): ^[a-zA-Z]\w$ Strong password(It must contain a combination of upper and lower case letters and numbers. Special characters cannot be used. The length is 8-10 between): ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).$ Date format:^\d-\d-\d 12 months of a year(01~09 And 1~12): ^(0?[1-9]|1[0-2])$ 31 days of a month(01~09 And 1~31): ^((0?[1-9])|((1|2)[0-9])|30|31)$ Input format of money: 1. There are four forms of money that we can accept:"10000.00" and "10,000.00", And No "branch" of "10000" and "10,000": ^[1-9][0-9]*$ 2. This indicates any number that does not start with 0,however,This also means one character"0"Fail,Therefore, we adopt the following form:^(0|[1-9][0-9]*)$ 3. A 0 or a number that does not begin with 0.We can also allow a minus sign at the beginning:^(0|-?[1-9][0-9]*)$ 4. This represents a 0 or a number that may be negative and does not start with 0.Let the user start with 0.Remove the minus sign,Because money can't be negative.What we want to add below is to explain the possible decimal parts:^[0-9]+(.[0-9]+)?$ 5. It must be noted that,There should be at least 1 digit after the decimal point,therefore"10."It doesn't pass,however "10" and "10.2" Yes:^[0-9]+(.[0-9])?$ 6. In this way, we stipulate that there must be two digits after the decimal point,If you think it's too harsh,You can do this:^[0-9]+(.[0-9])?$ 7. This allows the user to write only one decimal place.Now it's time to consider commas in numbers,We can do this:^[0-9](,[0-9])*(.[0-9])?$ 8. 1 To 3 numbers,Followed by any comma+3 Number,Comma becomes optional,Instead of having to:^([0-9]+|[0-9](,[0-9])*)(.[0-9])?$ Note: This is the final result,Don't forget"+"Can use"*"Instead, if you think an empty string is acceptable(strange,Why??)last,Don't forget to remove the backslash when using the function,Common mistakes are here xml File:^([a-zA-Z]+-?)+[a-zA-Z0-9]+\\.[x|X][m|M][l|L]$ Regular expressions for Chinese characters:[\u4e00-\u9fa5] Double byte character:[^\x00-\xff] (Including Chinese characters, it can be used to calculate the length of string(A double byte character length meter 2, ASCII Character meter 1)) Regular expression for blank line:\n\s*\r (Can be used to delete blank lines) HTML Regular expression for tag:<(\S*?)[^>]*>.*?</\1>|<.*? /> (The version circulated on the Internet is too bad. The above one is only partial, and there is still nothing to do with complex nested tags) Regular expression for leading and trailing white space characters:^\s*|\s*$or(^\s*)|(\s*$) (Can be used to delete white space characters at the beginning and end of a line(Including spaces, tabs, page breaks, and so on),Very useful expressions) tencent QQ number:[1-9][0-9] (tencent QQ The number starts at 10000) China Postal Code:[1-9]\d(?!\d) (The postal code of China is 6 digits) IP Address:\d+\.\d+\.\d+\.\d+ (extract IP Useful when addressing) IP Address:((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.)(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)) (from@Provided by Feilong sanshao,Thanks for sharing)

Reference article (invasion and deletion): the most complete collection of commonly used regular expressions

23 October 2021, 20:28 | Views: 1531

Add new comment

For adding a comment, please log in
or create account

0 comments