Home > Article > Web Front-end > A brief discussion on regular expressions on the front end
1. Overview
In JavaScript
, use //
You can create a regular expression object, of course you can also use new RegExp()
Commonly used methods related to regular expressions include match
, test
and replace
.
Among them, match
and replace
are methods on strings, and test
is a method on regular objects.
[Related course recommendations: JavaScript video tutorial]
Look at the specific illustrations below:
2. Match single characters
/reg/
can directly match specific strings reg
. The square brackets
##/[arzy]/
means matching any single character in
arzy The dash
-
/[f-h]/
represents matching
f to
in alphabetical order Any single character in h
represents matching numbers 1 to 3
3. Regular optionsThe regular object can also be followed by options,
JavaScript Commonly used options in are:
- represents ignoring case
- Represents multi-line matching
- Represents global matching (can match multiple times)
4. Boundary matching
- represents the beginning of the matching string
- Represents the end of the matched string
5. Character matching
non-
number
non-
whitespace character
[A-Za-z0-9_]
, which matches alphanumeric underscores
6. Quantifier matching
7. Grouping
() Parentheses represent grouping in regular expressions, usually in The match
method is used to return full matches plus multiple grouping results. If the g
option is used, only full matches are returned. In parentheses you can use the pipe symbol
, which stands for or
When matching special symbols, you need to add a backslash\
The special characters in JS are
^ $ \ . * ? () [] {} |
So if you need to match the asterisk
*
\*
match except a certain character Any characters need to be used in square brackets []
^So far
^
##10. End
The above only introduces the basic usage of regular expressions, which is basically enough for daily development. For more in-depth usage, such as Regular rules are very Powerful, but cannot be abused. If you write a very complicated regular expression that only you can understand, a better approach is to implement it without using regular expressions~ Reference link ● Intro to Regex for Web Developers This article comes from the js tutorial column, welcome to learn! Greedy and Lazy
, Zero-width Assertion
and Capture
, interested friends can learn by themselves~
The above is the detailed content of A brief discussion on regular expressions on the front end. For more information, please follow other related articles on the PHP Chinese website!