Home >Web Front-end >JS Tutorial >How to use regular expressions in JavaScript

How to use regular expressions in JavaScript

清浅
清浅Original
2018-11-24 10:21:245062browse

Today I will share with you the knowledge about regular expressions in JavaScript. It has certain reference value and I hope it will be helpful to everyone.

Regular expressions are also called regular expressions (Regular Expression) and are often abbreviated as regex, regexp or RE. Regular expressions are usually used to retrieve and replace text that matches a certain pattern (rule). That is, it can be used to check whether a string contains a certain substring, replace the matching substring, or extract the matching substring from a certain string. A substring of a certain condition, etc.

reg.test( ); Determine whether this string has fragments that meet the requirements. The return results are only true and false.

str.match( ); can match everything and return it. It is more intuitive than the previous method and will also tell us how many were returned.

Grammar

(1) Regular expression literal

is used to detect whether it contains The specified fragment

can be used to test whether the target string matches this template through the regex.test method

 var reg = /pattern/;
 例
var reg=/abc/;
var str="abcdef"

Image 1.jpg

But if str is changed to "str= "abdcef", the return value is false, because the order of the strings is different, so it does not match

(2) new RegExp()

for strings Search, match and replace

i: During the matching process, ignore case

var reg=/abc/i;
var str="abCdef"

Image 2.jpg

g: During the matching process, match the global

var reg=/abc/g;
var str="abccbaabcbcaacbabccbaabc"

Image 3.jpg

m: During the matching process, match multiple lines

var reg=/abc/m;
var str="abccbaabcbca\nacbabccbaabc"

Image 4.jpg

^: The matched string must start with the template

var reg=/^123457/;
var str="12345896567";

Image 6.jpg

Note: When ^ is outside the brackets, it means not

[]: matches (the range inside the brackets) one character

var reg=/[12345][12345]/;
var str="12345896567";

Image 5.jpg

(3) The metacharacter

in the regular expression means: a character with special meaning:

\w represents [ 0-9A-z_]

\W === [^\w]

\d represents [0-9]

\D===[^\ d]

\s represents white space characters

\S===[^\s]

\b represents word boundary

\B represents non Word boundary

(4) Greedy matching principle

n? Matches any string containing zero or one n. This variable is 0 or 1. Match.

n{X} matches a string containing ##n{X,Y} matches a string containing a sequence of X to Y n's

n{X, } matches a string containing a sequence of at least X n's

var reg=/\d{3}?/;
var str="12345896567";

Image 8.jpg

Summary: The above is a basic introduction to regular expression knowledge. I hope that through this article, everyone can understand regular expressions.

The above is the detailed content of How to use 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