A brief discussion on JS regular RegExp object

A brief discussion on JS regular RegExp object

1. RegExp object

  • Regular expressions are objects that describe character patterns.
  • Regular expressions are used to match string patterns and perform search and replacement. They are a powerful tool for performing pattern matching on strings.
  • Reference: w3cschool | JavaScript RegExp Object

2. Grammar

2.1 Definition

When creating a regular expression object using the constructor, normal character escape rules are required (prefixed with a backslash \)

/* For example, the following two definitions are equivalent*/

// Constructor method const reg = new RegExp("\\w+");
// Literal method const reg = /\w+/;

2.2 Modifiers

Used to perform case-sensitive and global matching

Modifiers describe
i Performs a case-insensitive match.
g Perform a global match (find all matches instead of stopping after the first one).
m Performs a multi-line match.

    /* chestnut*/
const reg = /\w/gi

2.3 Square brackets

Used to find characters within a range:

expression describe
[abc] Finds any character between the square brackets.
[^abc] Finds any character that is not between the square brackets.
[0-9] Finds any number from 0 to 9.
[az] Finds any character from lowercase a to lowercase z.
[AZ] Finds any character from uppercase A to uppercase Z.
[Az] Finds any character from uppercase A to lowercase z.
[adgk] Finds any character in the given set.
[^adgk] Finds any character outside the given set.
(red|blue|green) Finds any of the specified options.

/* chestnut*/
const reg = /[0-9]/g

2.4 Metacharacters

are characters with special meanings:

Metacharacters describe
. Searches for a single character, except newline and line terminator.
\w Find word characters.
\W Find non-word characters.
\d Find a number.
\D Find non-numeric characters.
\s Finds whitespace characters.
\S Finds non-whitespace characters.
\b Matches a word boundary.
\B Matches a non-word boundary.
\0 Finds a NUL character.
\n Finds a newline character.
\f Finds a form feed character.
\r Find the carriage return character.
\t Find the tab character.
\v Finds a vertical tab character.
\xxx Searches for the character specified by the octal number xxx.
\xdd Searches for the character specified by hexadecimal number dd.
\uxxxx Finds the Unicode character specified by the hexadecimal number xxxx.

/* chestnut*/
const reg = /\d/g // matches numbers

2.5 Quantifiers

are characters with special meanings:

quantifier describe
n+ Matches any string containing at least one n.
n* Matches any string containing zero or more occurrences of n.
n? Matches any string containing zero or one n.
n{X} Matches a string containing a sequence of X n's.
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.
n$ Matches any string ending with n.
^n Matches any string beginning with n.
?=n Matches any string that is immediately followed by the specified string n.
?!n Matches any string that is not immediately followed by the specified string n.

/* chestnut*/
const reg = /\d+/g // matches at least one digit

2.6 Methods

are characters with special meanings:

method describe FF IE
compile Compile a regular expression. 1 4
exec Retrieves the value specified in a string. Returns the found value and identifies its position. 1 4
test Retrieves the value specified in a string. Returns true or false. 1 4

method describe FF IE
search Retrieves values ​​that match a regular expression. 1 4
match Finds one or more matches of the regular expression. 1 4
replace Replaces substrings that match a regular expression. 1 4
split Split a string into an array of strings. 1 4

/* chestnut*/
var patt = /Hello/g
var result = patt.test(str) // Search for the string Hello -> true

This is the end of this article about the JS regular expression RegExp object. For more relevant JS regular expression RegExp object content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • A brief discussion on the use of RegExp objects and brackets in JS regular expressions
  • Detailed explanation of JavaScript regular expression RegExp object
  • RegExp object in JS regular expression
  • js regular expression explanation index attribute (RegExp object)
  • js regular expression input attribute ($_) RegExp object attribute introduction
  • js regular expression RegExp object attributes lastIndex, lastMatch, lastParen, lastContext, rightContext attribute explanation
  • JavaScript RegExp object (regular expression)

<<:  Detailed explanation of how to introduce custom fonts (font-face) in CSS

>>:  Hyperlink icon specifications: improve article readability

Recommend

Detailed explanation of how to copy and backup docker container data

Here we take the Jenkins container as an example ...

Some front-end basics (html, css) encountered in practice

1. The div css mouse hand shape is cursor:pointer;...

WeChat applet learning wxs usage tutorial

What is wxs? wxs (WeiXin Script) is a scripting l...

Tips for adding favicon to a website: a small icon in front of the URL

The so-called favicon, which is the abbreviation o...

Complete steps to use mock.js in Vue project

Using mock.js in Vue project Development tool sel...

Introduction to MySQL database performance optimization

Table of contents Why optimize? ? Where to start?...

MySQL transaction details

Table of contents Introduction Four characteristi...

Complete steps to enable gzip compression in nginx

Table of contents Preface 1. Configure gzip compr...

Do you know how to use Vue to take screenshots of web pages?

Table of contents 1. Install html2Canvas 2. Intro...

Sample code for realizing book page turning effect using css3

Key Takeaways: 1. Mastering CSS3 3D animation 2. ...

MySQL integrity constraints definition and example tutorial

Table of contents Integrity constraints Definitio...