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

How to use vuex in Vue project

Table of contents What is Vuex? Vuex usage cycle ...

Some notes on installing fastdfs image in docker

1. Prepare the Docker environment 2. Search for f...

A brief discussion on the Linux kernel's support for floating-point operations

Currently, most CPUs support floating-point units...

Detailed explanation of monitoring Jenkins process based on zabbix

1. Monitoring architecture diagram 2. Implementat...

Docker builds CMS on-demand system with player function

Table of contents text 1. Prepare the machine 2. ...

How to configure multiple projects with the same domain name in Nginx

There are two ways to configure multiple projects...

Vue3 encapsulates its own paging component

This article example shares the specific code of ...

HTML table tag tutorial (34): row span attribute ROWSPAN

In a complex table structure, some cells span mul...

Rsync+crontab regular synchronization backup under centos7

Recently, I want to regularly back up important i...

Analysis of the Neglected DOCTYPE Description

doctype is one of them: <!DOCTYPE HTML PUBLIC &...

Introduction to new ECMAscript object features

Table of contents 1. Object properties 1.1 Attrib...

Use of Linux network configuration tools

This article introduces RHEL8 network services an...

Insufficient memory problem and solution when docker starts elasticsearch

question Insufficient memory when docker installs...

How to solve the problem that mysql cannot be closed

Solution to mysql not closing: Right-click on the...