替换字符串教程替换字符串教程替换字符串教程替换字符串教程
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2024年3月5日
类别
  • 正则表达式
标签
替换字符串教程
  • 简
  • 繁
  • En
关于正则表达式 » 替换字符串教程

替换文本教学
简介
字符
不可打印字符
配对文本
反向引用
配对内容
大小写转换
条件
更多内容
简介
正则表达式快速开始
正则表达式教程
替换字符串教程
应用程序和语言
正则表达式范例
正则表达式参考
替换字符串参考

替换字符串教程

替换字符串,也称为替换文本,是在搜索和替换期间用来替换每个正则表达式配对的文本。在大部分应用程序中,替换文本支持特殊语法,让您可以重新使用正则表达式配对的文本或其部分内容。本教学说明此语法。虽然替换字符串与正则表达式相比相当简单,但不同应用程序使用的语法和实际行为仍有很大的差异。

在本网站中,替换字符串显示为 replace,就像您在应用程序的 [取代] 方块中输入它们一样。替换中的文本文本会以黄色突显。如 $&\$ 所示,特殊符号会以蓝色突显,而转义字符则以灰色突显。

目录

文本字符和特殊字符

最简单的替换文本只包含文本字符。某些字符在替换字符串中具有特殊意义,必须转义。在软件原代码中使用替换字符串时,转义规则可能会变得有点复杂。

不可打印字符

使用控制字符转义或十六进位转义,可以更轻松地输入不可打印字符,例如控制字符和特殊间距或换行字符。

配对文本

将整个正则表达式配对重新插入替换文本中,让搜索和替换可以在正则表达式配对前后插入文本,而不会实际替换任何内容。

反向引用

正则表达式中的 命名 和 编号捕获组 的反向引用,让替换文本可以重新使用正则表达式配对的文本部分。

配对内容

有些应用程序在替换字符串中支持特殊符号,让您可以插入主旨字符串或正则表达式配对前后的主旨字符串部分。当替换文本语法用于收集搜索配对及其内容,而不是在主旨字符串中进行替换时,这会很有用。

大小写转换

有些应用程序可以插入与正则表达式匹配的文本,或将捕获组转换为大写或小写。

条件

有些应用程序可以使用一个替换或另一个替换,具体取决于捕获组是否参与匹配。这允许您对正则表达式的不同匹配使用不同的替换。

替換字串教學
  • 简
  • 繁
  • En
關於正規表示式 » 替換字串教學

替換文字教學
簡介
字元
不可列印字元
配對文字
反向參照
配對內容
大小寫轉換
條件
本網站的更多資訊
簡介
正規表示式快速開始
正規表示式教學
替換字串教學
應用程式和語言
正規表示式範例
正規表示式參考
替換字串參考

替換字串教學

替換字串,也稱為替換文字,是在搜尋和替換期間用來替換每個正規表示式配對的文字。在大部分應用程式中,替換文字支援特殊語法,讓您可以重新使用正規表示式配對的文字或其部分內容。本教學說明此語法。雖然替換字串與正規表示式相比相當簡單,但不同應用程式使用的語法和實際行為仍有很大的差異。

在本網站中,替換字串顯示為 replace,就像您在應用程式的 [取代] 方塊中輸入它們一樣。替換中的文字文字會以黃色突顯。如 $&\$ 所示,特殊符號會以藍色突顯,而跳脫字元則以灰色突顯。

目錄

文字字元和特殊字元

最簡單的替換文字只包含文字字元。某些字元在替換字串中具有特殊意義,必須跳脫。在軟體原始碼中使用替換字串時,跳脫規則可能會變得有點複雜。

不可列印字元

使用控制字元跳脫或十六進位跳脫,可以更輕鬆地輸入不可列印字元,例如控制字元和特殊間距或換行字元。

配對文字

將整個正規表示式配對重新插入替換文字中,讓搜尋和替換可以在正規表示式配對前後插入文字,而不會實際替換任何內容。

反向參照

正規表示式中的 命名 和 編號擷取群組 的反向參照,讓替換文字可以重新使用正規表示式配對的文字部分。

配對內容

有些應用程式在替換字串中支援特殊符號,讓您可以插入主旨字串或正規表示式配對前後的主旨字串部分。當替換文字語法用於收集搜尋配對及其內容,而不是在主旨字串中進行替換時,這會很有用。

大小寫轉換

有些應用程式可以插入與正規表示式匹配的文字,或將擷取群組轉換為大寫或小寫。

條件

有些應用程式可以使用一個替換或另一個替換,具體取決於擷取群組是否參與匹配。這允許您對正規表示式的不同匹配使用不同的替換。

Replacement Strings Tutorial
  • 简
  • 繁
  • En
About Regular Expressions » Replacement Strings Tutorial

Replacement Text Tutorial
Introduction
Characters
Non-Printable Characters
Matched Text
Backreferences
Match Context
Case Conversion
Conditionals
More on This Site
Introduction
Regular Expressions Quick Start
Regular Expressions Tutorial
Replacement Strings Tutorial
Applications and Languages
Regular Expressions Examples
Regular Expressions Reference
Replacement Strings Reference

Replacement Strings Tutorial

A replacement string, also known as the replacement text, is the text that each regular expression match is replaced with during a search-and-replace. In most applications, the replacement text supports special syntax that allows you to reuse the text matched by the regular expression or parts thereof in the replacement. This tutorial explains this syntax. While replacement strings are fairly simple compared with regular expressions, there is still great variety between the syntax used by various applications and their actual behavior.

On this website, replacement strings are shown as replace like you would enter them in the Replace box of an application. Literal text in the replacement is highlighted in yellow. As $&\$ shows, special tokens are highlighted in blue and escaped characters in gray.

Table of Contents

Literal Characters and Special Characters

The simplest replacement text consists of only literal characters. Certain characters have special meanings in replacement strings and have to be escaped. Escaping rules may get a bit complicated when using replacement strings in software source code.

Non-Printable Characters

Non-printable characters such as control characters and special spacing or line break characters are easier to enter using control character escapes or hexadecimal escapes.

Matched Text

Reinserting the entire regex match into the replacement text allows a search-and-replace to insert text before and after regular expression matches without really replacing anything.

Backreferences

Backreferences to named and numbered capturing groups in the regular expression allow the replacement text to reuse parts of the text matched by the regular expression.

Match Context

Some applications support special tokens in replacement strings that allow you to insert the subject string or the part of the subject string before or after the regex match. This can be useful when the replacement text syntax is used to collect search matches and their context instead of making replacements in the subject string.

Case Conversion

Some applications can insert the text matched by the regex or by capturing groups converted to uppercase or lowercase.

Conditionals

Some applications can use one replacement or another replacement depending on whether a capturing group participated in the match. This allows you to use different replacements for different matches of the regular expression.

©2015-2025 艾丽卡 support@alaica.com