Home > Web Front-end > JS Tutorial > body text

How to use regular expressions in JS

php中世界最好的语言
Release: 2018-03-13 18:11:09
Original
1767 people have browsed it

This time I will bring you the regular expressions of JSHow to use it, what are the notes when using JS regular expressions, the following is a practical case, let’s take a look.

It is very important to learn regular expressions well. Here is some basic knowledge about regular expressions

\: Escape characters

^: Match the starting position of the string

$ : Match the end position of the string

* : Match the previous expression any number of times

+ : Match the previous expression one or more times

? : Match the previous expression zero or once

{n} : Match a certain n times, n is a non-negative integer

{n,} : Match at least n times, n is a non-negative integer Negative integer

{n,m}: Match at least n times and at most m times, n and m are both non-negative integers and n<=m

(*,+,{n, m})? : Non-greedy matching mode, as few matches as possible. For example, "z+" can match "zzzzz", "z+?" only matches "z"

. : Matches except \r\n Any single character except

(pattern): Match pattern and get the match. You can get the matched result, represented by \1-\9, for example, "(o)" matches "o", "( o)\1" matches "oo", "(\d{3})\1" matches "123123", but cannot match "123456"

(?:pattern) : non-getting match, Match pattern, but do not get the matching result

(?=pattern): non-get matching, positive positive pre-check, match the search string at the beginning of any string matching pattern, this match does not need to get the supply For future use, for example, "test(?=123)" can match "test" in "test123", but cannot match "test" in "test456"

(?!pattern) : non-getting match, Forward negative pre-search, matches the search string at the beginning of any string matching pattern. This match does not need to be obtained for later use. For example, "test(?=123)" can match "test" in "test456", but Cannot match "test" in "test123"

(?<=pattern): non-acquisition matching, reverse positive pre-check, similar to forward positive pre-check, in the opposite direction, such as "(?< =123)test" can match "test" in "123test", but cannot match "test" in "456test"

(?

x|y : Matches x or y

[xyz] : Matches any character contained

[^xyz] : Matches any character not contained

\b : Matches a word boundary, such as "on\b" can match "on" in "location", but cannot match "on" in "component"

\B: Matches non-word boundaries, such as "on" \B" can match "on" in "component", but cannot match "on" in "location"

\d : Matches a numeric character

\D : Matches non Numeric characters

\s : Matches any invisible characters, equivalent to [\f\n\r\t\v]

\S : Matches any visible characters, equivalent to [^\ f\n\r\t\v]

Learning regular rules can not only help us reduce the amount of code, but also solve many complex needs, so we must first lay a solid foundation before we can climb to higher levels

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Javascript’s singleton mode

Detailed explanation of flex layout

Javascript’s Observer Pattern

Javascript’s Agent Pattern

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template