


A concise summary of regular expressions in JavaScript_Basic knowledge
1. How to define regular expressions
There are two ways to define regular expressions: constructor definition and regular expression literal definition. For example:
through constructor var reg2 = /d{5, 12}/; // Define
Regular expression literal character
o: NUL character (u0000)
t: Tab character (u0009)
n: Line feed character (u000A)
v: Vertical tab character ( u000B)
f: Form feed character (u000C)
r: Carriage return character (u000D)
xnn: Latin character specified by the hexadecimal number nn. For example, x0A is equivalent to
uxxxx: Unicode character specified by hexadecimal number xxxx, for example, u0009 is equivalent to
🎜> ^: Matches the beginning of a string. In multi-line retrieval, matches the beginning of a line
$: Matches the end of a string. In multi-line retrieval, matches the end of a line
b: Matches a word The boundary, in short, is the position between the characters w and W, or the position between the character w and the beginning or end of the string ([b] matches the backspace character)
B: matches non- The position of the word boundary
(?=p): zero-width positive lookahead assertion, requiring the following characters to match p, but not including those characters that match p
(?!p): zero-width negative Assert to the lookahead, requiring that the following string does not match p
Regular expression character class
[...]: Any character within square brackets
[^...]: Not in square brackets Any character within brackets
.: Any character except newlines and other Unicode line terminators
w: Any word composed of ASCII characters, equivalent to [a-zA-Z0-9]
W: Any word that is not composed of ASCII characters, equivalent to [^a-zA-Z0-9]
s: Any Unicode whitespace character
S: Any non-Unicode whitespace character, pay attention to w and S Different
d: Any ASCII number, equivalent to [0-9]
D: Any character except ASCII digits, equivalent to [^0-9]
[b]: Backspace Direct quantity (special case)
Repeated character syntax of regular expression
{n, m}: Match the previous item at least n times, but not more than m times
{n, }: Match the previous item n times or more
{n}: Match the previous item n times
?: Match the previous item 0 or 1 times, which means the previous item is optional, equivalent to {0, 1}
: Matches the previous item 1 or more times, equivalent to {1, }
*: Matches the previous item 0 or more times, equivalent to {0, }
regular expression Selection, grouping and reference characters of expressions
|: Selection, matching the sub-expression on the left or the sub-expression on the right of the symbol
(...): Combination, combining several items into one unit, this Units can be modified by symbols such as "*", " ", "?" and "|", and the string matching this group can be remembered for any subsequent use
(?: ...): only Combination, combines items into one unit, but does not remember the characters that match the shuffling
n: Matches the first matching character of the nth group. The group is a subexpression in parentheses (it may also be Nested), the group index is the number of left brackets from left to right, grouping in the form of "(?:" is not encoded
Regular expression modifier
i: Perform case-insensitive matching
g: Perform a global match, in short, find all matches instead of stopping after finding the first one
m: Multi-line matching mode, ^ matches the beginning of a line and the beginning of a string, $ matches The end of the line and the end of the string
String method for pattern matching
search(): Its parameter is a regular expression, returning the starting position of the first matching substring. If If there is no matching substring, -1 is returned. If the parameter of search() is not a regular expression, it will first be converted to a regular expression through the RegExp constructor. search() does not support global retrieval because it ignores the modifier g. For example:
var s = "JavaScript".search(/script/i); // s = 4
replace(): It is used to perform retrieval and replacement. Receives two parameters, the first is the regular expression, and the second is the string to be replaced. If the modifier g is set in the regular expression, global replacement is performed, otherwise only the first matching substring is replaced. If the first argument is not a regular expression, the string is searched directly instead of being converted to a regular expression. For example:
Match(): Its parameter is a regular expression. If not, it is converted through RegExp and returns an array composed of matching results. If modifier g is set, a global match is performed. For example:
split(): This method is used to split the string that calls it into an array of substrings. The delimiter used is the parameter of split(), and its parameter can also be a regular expression. For example:
var d = '21, 123, 44, 64, 67, 3'.split(/s*, s*/); // d = ["21", "123", "44", "64", "67", "3"]
2. RegExp object
Each RegExp object has 5 attributes. The source attribute is a read-only string containing the text of the regular expression. The global attribute is a read-only Boolean value that indicates whether this regular expression has the modifier g. The attribute ignoreCase is a read-only Boolean value that indicates whether this regular expression has the modifier i. The multiline attribute is a read-only Boolean value that indicates whether this regular expression has the modifier m. The lastIndex attribute is a readable and writable integer. If the matching pattern has the g modifier, this attribute stores the starting position of the next search in the entire string.
The RegExp object has two methods. The parameter of exec() is a string, and its function is similar to match(). The exec() method executes a regular expression on a specified string, that is, performs a matching search in a string. If no match is found, null is returned. If a match is found, an array is returned. The first element of this array contains the string matching the regular expression, and the remaining elements are the subexpressions in parentheses. The matched substring, regardless of whether the regular expression has modifier g, will return the same array. When the regular expression object calling exec() has modifier g, it will set the lastIndex property of the current regular expression object to the character position immediately next to the matched substring. When exec() is called a second time with the same regular expression, it will start retrieving from the string indicated by the lastIndex attribute. If exec() does not find any matching results, it will reset lastIndex to 0. For example:
var text = "JavaScript is more fun than Java!"
var r;
while((r = p.exec(text)) != null) {
console.log(r, 'lastIndex: ' p .lastIndex);
}
Another method is test(). Its parameter is a string. Use test() to check a certain string. If it contains a matching result of the regular expression, it will return true otherwise it will return false. For example:
p. test('javascript'); // true

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP regular expression verification: Number format detection When writing PHP programs, it is often necessary to verify the data entered by the user. One of the common verifications is to check whether the data conforms to the specified number format. In PHP, you can use regular expressions to achieve this kind of validation. This article will introduce how to use PHP regular expressions to verify number formats and provide specific code examples. First, let’s look at common number format validation requirements: Integers: only contain numbers 0-9, can start with a plus or minus sign, and do not contain decimal points. floating point

In Go, you can use regular expressions to match timestamps: compile a regular expression string, such as the one used to match ISO8601 timestamps: ^\d{4}-\d{2}-\d{2}T \d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-][0-9]{2}:[0-9]{2})$ . Use the regexp.MatchString function to check if a string matches a regular expression.

To validate email addresses in Golang using regular expressions, follow these steps: Use regexp.MustCompile to create a regular expression pattern that matches valid email address formats. Use the MatchString function to check whether a string matches a pattern. This pattern covers most valid email address formats, including: Local usernames can contain letters, numbers, and special characters: !.#$%&'*+/=?^_{|}~-`Domain names must contain at least One letter, followed by letters, numbers, or hyphens. The top-level domain (TLD) cannot be longer than 63 characters.

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

The method of using regular expressions to verify passwords in Go is as follows: Define a regular expression pattern that meets the minimum password requirements: at least 8 characters, including lowercase letters, uppercase letters, numbers, and special characters. Compile regular expression patterns using the MustCompile function from the regexp package. Use the MatchString method to test whether the input string matches a regular expression pattern.

PHP is a widely used programming language, especially popular in the field of web development. In the process of web development, we often encounter the need to filter and verify text input by users, among which character filtering is a very important operation. This article will introduce how to use regular expressions in PHP to implement Chinese character filtering, and give specific code examples. First of all, we need to clarify that the Unicode range of Chinese characters is from u4e00 to u9fa5, that is, all Chinese characters are in this range.

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

PHP Regular Expressions: Exact Matching and Exclusion Fuzzy inclusion regular expressions are a powerful text matching tool that can help programmers perform efficient search, replacement and filtering when processing text. In PHP, regular expressions are also widely used in string processing and data matching. This article will focus on how to perform exact matching and exclude fuzzy inclusion operations in PHP, and will illustrate it with specific code examples. Exact match Exact match means matching only strings that meet the exact condition, not any variations or extra words.
