Home >Web Front-end >JS Tutorial >The meaning and use of regular expressions in JavaScript

The meaning and use of regular expressions in JavaScript

黄舟
黄舟Original
2017-11-09 16:52:041971browse

I believe that many people know regular expressions, but many people’s first impression is that it is difficult to learn, because at first glance, they feel that there is no pattern at all, and it is all a bunch of various All kinds of special symbols are completely incomprehensible. In fact, regular expressions are not as difficult as you think. Today we will take you to quickly understand JavaScriptregular expressions!

1. What is a regular expression?

Regular expression is a special The string pattern is used to match a set of strings, which is like using a mold to make a product, and regular rules are this mold, defining a rule to match characters that match the rules.

Regular expression (regular expression) describes a string matching pattern (pattern), which can be used to check whether a string contains a certain substring, replace the matching substring, or select from a certain string. Get substrings that meet certain conditions, etc.

To put it bluntly, regular expressions are used to process strings. We can use it to process some complex strings.

2. Regular expression rules

##1.1 Ordinary characters

Letters, numbers, Chinese characters, underscores, and punctuation marks not specifically defined in the following chapters are all "ordinary characters". Ordinary characters in an expression, when matching a string, match the same character.


Example 1: The expression "c", when matching the string "abcde", the matching result is: success; the matched content is: "c"; the matched position is: starting at 2, Ended at 3. (Note: Whether the subscript starts from 0 or 1 may differ depending on the current
programming language)
Example 2: The expression "bcd" matches the string "abcde" ", the matching result is: success; the matched content is: "bcd"; the matched position is: starting at 1 and ending at 4.

3. Special characters in regular expressions

Character meaning

\ is used as a change of meaning, that is, the characters after "\" are usually not interpreted according to their original meaning. For example, /b/ matches the character "b". When a backslash is added in front of b, /\b/ will change the meaning. To match a word boundary.
-or-
Restoration of regular expression functional characters, such as "*" matching the metacharacter before it 0 or more times, /a*/ will match a, aa, aaa, with "\" added After that, /a\*/ will only match "a*".

^ matches an input or the beginning of a line, /^a/ matches "an A", but does not match "An a"
$ matches an input or the end of a line, /a$/ matches " An a", not matching "an A"
* Matches the preceding metacharacter 0 or more times, /ba*/ will match b,ba,baa,baaa
+ Matches the preceding metacharacter 1 or more times times, /ba*/ will match ba,baa,baaa
? Match the preceding metacharacter 0 or 1 times, /ba*/ will match b,ba
(x) Match x and save x in a file named $1 ...in the variable of $9
x|y matches x or y
{n} matches exactly n times
{n,} matches n or more times
{n,m} matches n-m times
[xyz] character set, matches any character (or metacharacter) in this set
[^xyz] does not match any character in this set
[\b] Matches a backspace character
\b Matches a word boundary
\B Matches a non-word boundary
\cX Here, X is a control character, /\cM/ matches Ctrl-M
\d matches an alphanumeric character, /\d/ = /[0-9]/
\D matches a non-alphanumeric character, /\D/ = /[^0-9]/
\n matches a Line feed character
\r matches a carriage return character
\s matches a blank character, including \n,\r,\f,\t,\v, etc.
\S matches a non-blank character, equal to /[^\n\f\r\t\v]/
\t Matches a tab character
\v Matches a double tab character
\w Matches a character that can form a word ( alphanumeric, this is my free translation, including numbers), including underscores, such as [\w] matches the 5 in "$5.98", which is equal to [a-zA-Z0-9]
\W matches a word that cannot be formed Characters, such as [\W] matches the $ in "$5.98", which is equal to [^a-zA-Z0-9].

4. Basic syntax of regular expressions

Two special symbols '^' and ' $'. Their function is to indicate the beginning and end of a string respectively.

Examples are as follows:

"^The": represents all strings starting with "The" ("There", "The cat", etc.);

"of despair$": indicates a string ending with "of despair";

"^abc$": indicates a string starting and ending with "abc" - haha, there is only "abc" itself. ;

"notice": Represents any string containing "notice".

Like the last example, if you don't use two special characters, you are indicating that the string you want to find is in any part of the searched string - you are not
positioning it at the top of a certain .

Other symbols include '*', '+' and '?', which represent the number of times a character or a sequence of characters appears repeatedly.

They mean "none or more", "once or more" and "none or once" respectively.

Here are a few examples:

"ab*": Indicates that a string has an a followed by zero or several b. ("a", "ab", "abbb",...);

"ab+": Indicates that a string has an a followed by at least one b or more;

" ab?": Indicates that a string has an a followed by zero or one b;

"a?b+$": Indicates that there is zero or one a followed by one or several b at the end of the string .

You can also use a range, enclosed in curly brackets, to indicate the range of repetitions.

"ab{2}": Indicates that a string has an a followed by 2 b ("abb");

"ab{2,}": Indicates that a string has a a followed by at least 2 b;

"ab{3,5}": Indicates that a string has an a followed by 3 to 5 b.

Please note that you must specify the lower limit of the range (eg: "{0,2}" instead of "{,2}").

Also, you may have noticed that '*', '+' and '?' are equivalent to "{0,}", "{1,}" and "{0,1}".

There is also a '¦', which means "or" operation:

"hi¦hello": indicates that there is "hi" or "hello" in a string;

"(b¦cd)ef": represents "bef" or "cdef";

"(a¦b)*c": represents a string of mixed "a" and "b" followed by A "c";

'.' can replace any character:

"a.[0-9]": Indicates that a string has an "a" followed by an arbitrary character and A number;

"^.{3}$": represents a string of any three characters (length is 3 characters);

Square brackets indicate that certain characters are allowed to appear at a specific position in a string:

"[ab]": Indicates that a string has an "a" or "b" (equivalent to "a¦b");

"[a-d]": Indicates that a string contains one of lowercase 'a' to 'd' (equivalent to "a¦b¦c¦d" or " [abcd]");

"^[a-zA-Z]": represents a string starting with a letter;

"[0-9]%": represents a hundred There is one digit before the semicolon;

",[a-zA-Z0-9]$": indicates that a string ends with a comma followed by a letter or number.

You can also use '^' in square brackets to indicate unwanted characters. '^' should be the first one in the square brackets.

(For example: "%[^a-zA-Z]%" means that letters should not appear between two percent signs).

In order to express verbatim, you must add the transfer character '\' before the characters "^.$()¦*+?{\".

Please note that within square brackets, no escape characters are required.

Summary

In fact, I just don’t understand regular expressions, so I understand. You will find that this is it. There are not many related characters used in regular expressions, and they are not difficult to remember or understand. The only difficulty is that after combining them, the readability is relatively poor and it is not easy to understand. This article It is designed to let everyone have a basic understanding of regular expressions, be able to understand simple regular expressions, and write simple regular expressions to meet the needs of daily development.

Related recommendations:

What are the commonly used regular expressions in js

JavaScript regular expression video tutorial

Definition and introduction of javascript regular expression

How to be flexibleUse JavaScriptRegular expression

The above is the detailed content of The meaning and use of regular expressions in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

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