This article describes the common usage of JS regular expressions. Share it for your reference, as follows:
Preface: I have repeatedly studied regular expressions many times, and I have forgotten them after learning them, and I have forgotten them again and again. This time I plan to organize all the basics to strengthen memory and facilitate the next query.
Before learning regular expressions, you must first master and memorize these basic concepts:
1. Metacharacter: (.,\w,\W,\d,\D,\s,/S,^,$,)
character | meaning |
---|---|
. | Match any character except line breaks. |
\s | Represents any whitespace character (line breaks, tabs, spaces) |
\S | Match any non-empty string |
\b | Match word boundaries, match the beginning and end of the word. |
\B | Match a non-word boundary |
\d | Match a number, equivalent to [0-9] |
\D | Match a number, equivalent to [^0-9] |
\w | Matching a single character (letter, number, or underscore) is equivalent to [A-Za-z0-9_], for example, /\w/ matches 'a' in "apple," , '5' in "$5.28," and '3' in "3D." \W Matches a non-single character. Equivalent to [^A-Za-z0-9_], for example, /\W/ or /[^A-Za-z0-9_]/ matches '%' in "50%.". |
\W | Match a non-single character. Equivalent to [^A-Za-z0-9_], for example, /\W/ or /[^A-Za-z0-9_]/ matches '%' in "50%.". |
^ | The beginning of the matching string is used in [] brackets to indicate exclusion. /^A/ will not match 'A' in "an A", but will match 'A' in "An E". |
$ | Match the end of the string. For example, /t$/ does not match 't' in "eater", but will match 't' in "eat". When the QQ number must be 5 to 12 digits, you can use:^\d{5,12}$ |
We found that \W capital letters all mean the opposite.
2. Grouping characters ([],(),{});
character | meaning |
---|---|
() | What should I do if I want to repeat multiple characters? You can use brackets to specify the subexpression (also called grouping), and then you can specify the number of repetitions of this subexpression. eg: (\d{1,3}\.){3}\d{1,3} is a simple IP address matching expression. To understand this expression, analyze it in the following order: \d{1,3} matches 1 to 3 digits, (\d{1,3}\.){3} matches three digits and adds an English period (this whole is also this grouping) and repeats it 3 times, and finally adds a one to three digits (\d{1,3}). |
[] | Represents a collection of characters. Match any character in the brackets, including escape sequences. You can use dash (-) to specify a range of characters. Special symbols such as dots (.) and asterisks (*) have no special meaning in a character set. They don't have to escape, but escape also works. For example, [abcd] and [a-d] are the same. They all match the 'b' in "brisket" and the 'c' in "city". /[a-z.]+/ and /[\w.]+/ both match all characters in "". |
{} | Represents the range of quantifiers. |
3. Modifier (i,g,m);
character | meaning |
---|---|
i | Ignore case |
g | Perform global matching |
m | Perform multi-line matching |
4. Quantifiers (*,?,+,-,{n,m},?=n,?!=n);
character | meaning |
---|---|
* | Match any time, .* is connected together means any number of characters that do not contain newlines. Equivalent to {0,} |
+ | Match the previous expression once or multiple times. Equivalent to {1,}. |
? | Match the previous expression 0 or 1 time. Equivalent to {0,1}. |
{n,m} | Match n to m times |
x(?=y) | Matching 'x' is only if 'x' is followed by 'y'. This is called positive positive search. For example, /Jack(?=Sprat)/ will match 'Jack' only if it is followed by 'Sprat'. /Jack(?=Sprat|Frost)/Match 'Jack' only if it is followed by 'Sprat' or 'Frost'. But neither 'Sprat' nor 'Frost' are part of the matching result. |
x(?!y) | Matching 'x' is just if 'x' is not followed by 'y', this is called positive negative search. For example, /\d+(?!\.)/ matches a number only if the number is not followed by the decimal point. Regular expression /\d+(?!\.)/.exec("3.141") matches '141' but not '3.141' |
Example analysis:
var reg = /\(?0\d{2}[) -]?\d{8}/
"(" and ")" are also metacharacters, which will be mentioned in the grouping sections later, so escapes are needed here.
This expression can match phone numbers in several formats, such as (010) 88886666, or 022-22334455, or 02912345678, etc. Let's do some analysis of it: first, it is an escape character \(, it can appear 0 times or 1 times (?), then a 0, followed by 2 numbers (\d{2}), then) or - or one of the spaces, it appears 1 times or does not appear (?), and finally 8 numbers (\d{8}).
The following is an example to explain RegExp:
Requirement 1: Match a word of hi in one sentence.
var str = 'Hi RegExp I love you so much Hi Hi hi'; var reg = new RegExp("\\bhi\\b","gi");//g is a modifier that indicates global matching. \b is a metacharacter that represents the word boundary and matches the beginning and end of the word.//Direct quantity syntax:reg2 = /\bhi\b/gi; ((reg2));//['hi','hi','hi','hi'] //upgrade//There is a lucy not far behind the matching hivar strlc = /\bhi\b.*\blucy\b/; var luch = 'hi welcome to beijing lucy!!!'; ((strlc));
Requirement 2: Match a word of hi in one sentence.
var reg = /0\d\d\d-\d\d\d\d\d\d\d\d/;//\d represents a number, equivalent to [0-9],\D matches a non-numeric character, equivalent to [^0-9]var tel = "0123-887523146"; ((reg));//0123-88752314; //This method of writing multiple times in a row is stupid, so variables are introduced.regTel = /0\d{3}-\d{8}/; ((regTel));//0123-88752314;
Requirement 3: Write a regular expression that clears the spaces before and after the string.
= function(){return (/(^\s*)|(\s*$)/g, "");} var str2 = " hi space "//There are two spaces in total here();//14 (().length);//8 (());//hi space
Requirement 4: Match an email.
var eReg = /\S*@\S*\.\S*/; (('873619879@'))//true
Reference article:
https:///books/
/zh-CN/docs/Web/JavaScript/Guide/Regular_Expressions#
///article/
https:///article/
PS: Here are two very convenient regular expression tools for your reference:
JavaScript regular expression online testing tool:
http://tools./regex/javascript
Regular expression online generation tool:
http://tools./regex/create_reg
For more information about JavaScript, readers who are interested in reading this site's special topic:JavaScript regular expression skills》、《Summary of JavaScript replacement operation skills》、《Summary of JavaScript search algorithm skills》、《Summary of JavaScript data structure and algorithm techniques》、《JavaScript traversal algorithm and skills summary》、《Summary of json operation skills in JavaScript》、《Summary of JavaScript Errors and Debugging Skills"and"Summary of JavaScript mathematical operations usage》
I hope this article will be helpful to everyone's JavaScript programming.