Table of Contents
Using regular expressions in JavaScript
Backslashes in regular expressions
使用括号来记住匹配
将标志与正则表达式结合使用
最终想法
Home CMS Tutorial WordPress A beginner's guide to regular expressions in JavaScript

A beginner's guide to regular expressions in JavaScript

Aug 30, 2023 pm 03:05 PM
javascript regular expression Beginner's Guide

A beginners guide to regular expressions in JavaScript

Everyone who uses JavaScript has to deal with strings at some point. Sometimes you just store the string in another variable and pass it around. Other times you have to inspect it and see if it contains a specific substring.

However, things are not always easy. Sometimes you are not looking for a specific substring, but rather a set of substrings that follow a specific pattern.

Suppose you have to replace all occurrences of "Apples" in a string with "apples". You can simply use theMainString.replace("Apples", "apples"). Good and easy.

Now assume you also have to replace "appLes" with "apples". Likewise, "appLES" should become "apples". Basically, all case variations of "Apple" need to be changed to "apple". In this case, passing a simple string as an argument is no longer practical or efficient.

This is where regular expressions come in - you can simply use the case-insensitive flag i and be done with it. With this flag, it doesn't matter whether the original string contains "Apples", "APPles", "ApPlEs", or "Apples". Every instance of the word will be replaced with "apples".

Just like the case-insensitive flag, regular expressions provide many other features, which this tutorial will cover.

Using regular expressions in JavaScript

You must use slightly different syntax to indicate regular expressions within different String methods. Unlike a simple string enclosed in quotes, a regular expression consists of a pattern enclosed between slashes. Any flags you use in the regular expression will be appended after the second slash.

Back to the previous example, here's what the replace() method looks like using regular expressions and simple strings.

"I ate Apples".replace("Apples", "apples");
// I ate apples

"I ate Apples".replace(/Apples/i, "apples");
// I ate apples

"I ate aPPles".replace("Apples", "apples");
// I ate aPPles

"I ate aPPles".replace(/Apples/i, "apples");
// I ate apples
Copy after login

As you can see, the regular expression works in both cases. We will now learn more about the flags and special characters that make up patterns within regular expressions.

Backslashes in regular expressions

You can convert normal characters to special characters by adding a backslash before them. Likewise, you can convert special characters to normal characters by adding a backslash before them.

For example, d is not a special character. However, \d is used to match numeric characters in a string. Likewise, D is not a special character, but \D is used to match non-numeric characters in a string.

Numeric characters include 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9. When you use \d in a regular expression, it will match any of the following nine characters. When you use \D in a regular expression, it will match all non-numeric characters.

The following example should make things clear.

"L8".replace(/\d/i, "E");
// LE

"L8".replace(/\D/i, "E");
// E8

"LLLLL8".replace(/\D/i, "E");
// ELLLL8
Copy after login

You should note that in the third case, only the first matching character is replaced. You can also use flags to replace all matches. We will learn about such flags later.

Just like \d and \D, there are other special character sequences.

  1. You can use \w to match any "word" character in a string. Here, word characters refer to A-Z, a-z, 0-9, and _. So basically, it will match all numbers, all lowercase and uppercase letters, and underscores.
  2. You can use \W to match any non-word character in a string. It will match characters like %, $, #, $, etc.
  3. You can use \s to match a single whitespace character, which includes space, tab, formfeed, and newline characters. Likewise, you can use \S to match all other characters except spaces.
  4. You can also use \f, \n, \r, \t and \v , representing page feed, line feed, carriage return, horizontal tab and vertical tab respectively.

Sometimes you'll encounter a situation where you need to replace a word with an alternative, but only if it is not part of a larger word. For example, consider the following sentences:

"There are tons of pineapple pictures posted on the app."

In this example, we want to replace the word "app" with "board". However, using a simple regex pattern would turn "apple" into "boardle" and the final sentence would become:

"There are many pine board images posted on the app".

在这种情况下,您可以使用另一个特殊字符序列:\b。这会检查单词边界。单词边界是通过使用任何非单词字符(如空格、“$”、“%”、“#”等)形成的。不过请注意,它还包括重音字符,如“ü”。

"A lot of pineapple images were posted on the app".replace(/app/, "board");
// A lot of pineboardle images were posted on the app

"A lot of pineapple images were posted on the app".replace(/\bapp/, "board");
// A lot of pineapple images were posted on the board
Copy after login

同样,您可以使用 \B 来匹配非单词边界。例如,您可以使用 \B 仅匹配位于另一个单词(如“pineapple”)内的“app”。

匹配模式“n”次

您可以使用 ^ 告诉 JavaScript 仅查看字符串的开头以进行匹配。同样,您可以使用 $ 仅查看字符串末尾的匹配项。

您可以使用 * 来匹配前面的表达式 0 次或多次。例如,/Ap*/ 将匹配 AApAppAppp,等等。

类似地,您可以使用 + 来匹配前面的表达式 1 次或多次。例如,/Ap+/ 将匹配 ApAppAppp 等。这次表达式将不会匹配单个 A

有时,您只想匹配给定模式的特定出现次数。在这种情况下,您应该使用 {n} 字符序列,其中 n 是数字。例如,/Ap{2}/ 将匹配 App,但不匹配 Ap。它还将匹配 Appp 中的前两个“p”,并保持第三个“p”不变。

您可以使用 {n,} 来匹配给定表达式的至少“n”次出现。这意味着 /Ap{2,}/ 将匹配 App,但不匹配 Ap。它还将匹配 Apppp 中的所有“p”,并将它们替换为您的替换字符串。

您还可以使用 {n,m} 指定最小和最大数量,并限制给定表达式应匹配的次数。例如,/Ap{2,4}/ 将匹配 AppApppApppp。它还将匹配 Apppppp 中的前四个“p”,并保持其余部分不变。

"Apppppples".replace(/Ap*/, "App");
// Apples

"Ales".replace(/Ap*/, "App");
// Apples

"Appppples".replace(/Ap{2}/, "Add");
// Addppples

"Appppples".replace(/Ap{2,}/, "Add");
// Addles

"Appppples".replace(/Ap{2,4}/, "Add");
// Addples
Copy after login

使用括号来记住匹配

到目前为止,我们只用常量字符串替换了模式。例如,在上一节中,我们使用的替换始终是“Add”。有时,您必须在给定字符串中查找模式匹配,然后将其替换为模式的一部分。

假设您必须在字符串中找到一个包含五个或更多字母的单词,然后在该单词的末尾添加一个“s”。在这种情况下,您将无法使用常量字符串值作为替换,因为最终值取决于匹配模式本身。

"I like Apple".replace(/(\w{5,})/, '$1s');
// I like Apples

"I like Banana".replace(/(\w{5,})/, '$1s');
// I like Bananas
Copy after login

这是一个简单的示例,但您可以使用相同的技术在内存中保留多个匹配模式。完整匹配中的子模式数量将由使用的括号数量决定。

在替换字符串中,第一个子匹配将使用 $1 标识,第二个子匹配将使用 $2 标识,依此类推。这是另一个例子,进一步阐明括号的用法。

"I am looking for John and Jason".replace(/(\w+)\sand\s(\w+)/, '$2 and $1');
// I am looking for Jason and John
Copy after login

将标志与正则表达式结合使用

正如我在简介中提到的,正则表达式的一个更重要的功能是使用特殊标志来修改搜索的执行方式。这些标志是可选的,但您可以使用它们来执行诸如全局搜索或不区分大小写之类的操作。

这些是四个常用的标志,用于更改 JavaScript 搜索或替换字符串的方式。

  • g:该标志将执行全局搜索,而不是在第一个匹配后停止。
  • i:此标志将执行搜索,而不检查大小写是否完全匹配。例如,在不区分大小写的搜索中,Apple、aPPLe 和 apPLE 的处理方式都是相同的。
  • m:该标志将执行多行搜索。
  • y:此标志将在 lastIndex 属性指示的索引中查找匹配项。

以下是与标志一起使用的正则表达式的一些示例:

"I ate apples, you ate apples".replace(/apples/, "mangoes");
// "I ate mangoes, you ate apples"

"I ate apples, you ate apples".replace(/apples/g, "mangoes");
// "I ate mangoes, you ate mangoes"

"I ate apples, you ate APPLES".replace(/apples/, "mangoes");
// "I ate mangoes, you ate APPLES"

"I ate apples, you ate APPLES".replace(/apples/gi, "mangoes");
// "I ate mangoes, you ate mangoes"


var stickyRegex = /apples/y;
stickyRegex.lastIndex = 3;
"I ate apples, you ate apples".replace(stickyRegex, "mangoes");
// "I ate apples, you ate apples"

var stickyRegex = /apples/y;
stickyRegex.lastIndex = 6;
"I ate apples, you ate apples".replace(stickyRegex, "mangoes");
// "I ate mangoes, you ate apples"

var stickyRegex = /apples/y;
stickyRegex.lastIndex = 8;
"I ate apples, you ate apples".replace(stickyRegex, "mangoes");
// "I ate apples, you ate apples"
Copy after login

最终想法

本教程的目的是向您介绍 JavaScript 中的正则表达式及其重要性。我们从基础知识开始,然后介绍反斜杠和其他特殊字符。我们还学习了如何检查字符串中的重复模式以及如何记住模式中的部分匹配以便以后使用它们。

最后,我们了解了常用的标志,它们使正则表达式变得更加强大。您可以在 MDN 上的这篇文章中了解有关正则表达式的更多信息。

If you want me to clarify anything in this tutorial, please feel free to let me know in the comments.

The above is the detailed content of A beginner's guide to regular expressions in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP regular expression validation: number format detection PHP regular expression validation: number format detection Mar 21, 2024 am 09:45 AM

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

How to validate email address in Golang using regular expression? How to validate email address in Golang using regular expression? May 31, 2024 pm 01:04 PM

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.

How to match timestamps using regular expressions in Go? How to match timestamps using regular expressions in Go? Jun 02, 2024 am 09:00 AM

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.

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

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

PHP regular expressions: exact matching and exclusion of fuzzy inclusions PHP regular expressions: exact matching and exclusion of fuzzy inclusions Feb 28, 2024 pm 01:03 PM

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.

How to verify password using regular expression in Go? How to verify password using regular expression in Go? Jun 02, 2024 pm 07:31 PM

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.

Chinese character filtering: PHP regular expression practice Chinese character filtering: PHP regular expression practice Mar 24, 2024 pm 04:48 PM

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.

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

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

See all articles