XRegExp JavaScript 正则表达式函数库XRegExp JavaScript 正则表达式函数库XRegExp JavaScript 正则表达式函数库XRegExp JavaScript 正则表达式函数库
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2024年3月5日
类别
  • 正则表达式
标签
XRegExp JavaScript 正则表达式函数库
  • 简
  • 繁
  • En
关于正则表达式 » 正则表达式参考 » 替换字符串参考 » 替换字符串参考:符合文本与反向引用

正则表达式工具
grep
语言和函数库
Boost
Delphi
GNU (Linux)
Groovy
Java
JavaScript
.NET
PCRE (C/C++)
PCRE2 (C/C++)
Perl
PHP
POSIX
PowerShell
Python
R
Ruby
std::regex
Tcl
VBScript
Visual Basic 6
wxWidgets
XML Schema
Xojo
XQuery 和 XPath
XRegExp
数据库
MySQL
Oracle
PostgreSQL
本网站更多内容
简介
正则表达式快速入门
正则表达式教程
替换字符串教程
应用程序和语言
正则表达式范例
正则表达式参考
替换字符串参考

XRegExp JavaScript 正则表达式函数库

XRegExp 是由 Steven Levithan 开发的开源 JavaScript 函数库。它支持所有现代浏览器,以及许多较旧甚至古老的浏览器版本。它也可以在服务器上使用 Node.js。您可以在 xregexp.com 下载 XRegExp。

使用 XRegExp 对象取代 JavaScript 内置的 RegExp 对象,可以提供具备更多功能且跨浏览器差异较少的正则表达式语法。添加的显著功能包括 自由间距、命名截取、模式修改器 和 Unicode 类别、区块和脚本。它也会将无效的转义字符和不存在的反向引用视为错误。

XRegExp 也提供自己的 replace() 方法,其替换文本语法已使用 命名反向引用 增强,且没有跨浏览器差异。它也提供完全符合 JavaScript 标准的 split() 方法。

要使用 XRegExp,首先使用 var myre = XRegExp('regex', 'flags') 创建一个正则表达式对象,其中 flags 是字母 g(全域)、i(不分大小写)、m(锚点 在换行符号处比对)、s(点 比对换行符号)、x(自由间距)和 n(明确截取)的组合。XRegExp 3 添加 A(星体)旗标,在比对 Unicode 属性和区块时,会包含 U+FFFF 之外的 Unicode 字符。ECMAScript 6 旗标 y(黏着)和 u(Unicode)也可以用在原生支持这些旗标的现代浏览器中,但如果浏览器没有内置支持这些旗标,就会掷回错误。

然后你可以将创建的 XRegExp 运行个体传递给各种 XRegExp 方法。请务必按照以下所示进行调用,才能获得完整的 XRegExp 功能。XRegExp 构造函数传回的对象是原生 JavaScript RegExp 对象。该对象的方法是浏览器内置的 RegExp 方法。你可以通过调用 XRegExp.install('natives') 来使用 XRegExp 的方法取代内置的 RegExp 方法。这么做也会影响由一般 RegExp 构造函数或双斜线正则表达式字面值创建的 RegExp 对象。

XRegExp.test(str, regex, [pos=0], [sticky=false]) 测试 regex 是否可以比对字符串的一部分。pos 参数是字符串中比对尝试应开始的从 0 开始的索引。如果你传递 true 或 'sticky' 给 sticky 参数,则只会在 pos 尝试比对。这类似于在其他风格中将 开始尝试锚点 \G(XRegExp 不支持)添加到 regex 的开头。

XRegExp.exec(str, regex, [pos=0], [sticky=false]) 的功能与 XRegExp.test() 相同,但会传回 null 或数组,而不是 false 或 true。数组中的索引 0 保存整体 regex 比对。索引 1 和之后的索引保存由捕获组比对的文本(如果有)。如果 regex 有命名捕获组,则其比对会在 XRegExp 4 及之前版本中作为数组上的属性提供。在 XRegExp 5 中,数组有一个 group 属性,该属性会将捕获组的名称作为属性。XRegExp.exec() 不依赖 lastIndex 属性,因此可以避免与该属性相关的跨浏览器问题。

XRegExp.forEach(str, regex, callback) 可以轻松地反复运行字符串中 regex 的所有比对。它会反复运行所有比对,而不论 global 旗标和 lastIndex 属性。会使用四个参数调用 callback。前两个是类似 exec() 传回的数组,以及比对在字符串中开始的索引。最后两个是 str 和 regex,与你传递给 forEach() 的内容完全相同。

XRegExp.replace(str, regex, replacement, [scope]) 传回一个字符串,其中 str 中 regex 的比对已用 replacement 取代。传递 'one' 或 'all' 作为 scope 参数,以只取代第一个比对或所有比对。如果你省略 scope 参数,则 regex.global 旗标会决定是否只取代第一个或所有比对。

「XRegExp.replace()」方法使用自己的替换文本语法。它与原生 JavaScript 语法非常相似。它在某种程度上不兼容,会将无法形成有效替换代码的美元符号视为错误。但好处是它消除了所有浏览器间的不一致性。 $$ 会插入一个单一的文本美元符号。 $& 和 $0 会插入 整体正则表达式比对。 $` 和 $' 会插入正则表达式比对左右两侧的 主旨字符串部分。 $n、$nn、${n} 和 ${nn} 是 编号反向引用,而 ${name} 是 命名反向引用。

如果你传递一个函数作为 replacement 参数,则会以三个或更多参数调用它。第一个参数是已比对的字符串,其中命名的捕获组可通过该字符串上的属性取得。第二个和后续参数是正则表达式中每个捕获组比对到的字符串(如果有的话)。最后两个参数是找到比对的字符串索引和原始主旨字符串。

XRegExp.split(str, regex, [limit]) 是 String.prototype.split 的替代方案。它精确遵循 JavaScript 的字符串分割标准,消除了所有浏览器间的不一致性和错误。

XRegExp JavaScript 正則表達式函式庫
  • 简
  • 繁
  • En
關於正規表示式 » 正規表示式參考 » 替換字串參考目錄 » 替換字串參考:符合文字與反向參照

正規表示式工具
grep
語言和函式庫
Boost
Delphi
GNU (Linux)
Groovy
Java
JavaScript
.NET
PCRE (C/C++)
PCRE2 (C/C++)
Perl
PHP
POSIX
PowerShell
Python
R
Ruby
std::regex
Tcl
VBScript
Visual Basic 6
wxWidgets
XML Schema
Xojo
XQuery 和 XPath
XRegExp
資料庫
MySQL
Oracle
PostgreSQL
本網站更多內容
簡介
正規表示式快速入門
正規表示式教學
替換字串教學
應用程式和語言
正規表示式範例
正規表示式參考
替換字串參考

XRegExp JavaScript 正則表達式函式庫

XRegExp 是由 Steven Levithan 開發的開源 JavaScript 函式庫。它支援所有現代瀏覽器,以及許多較舊甚至古老的瀏覽器版本。它也可以在伺服器上使用 Node.js。您可以在 xregexp.com 下載 XRegExp。

使用 XRegExp 物件取代 JavaScript 內建的 RegExp 物件,可以提供具備更多功能且跨瀏覽器差異較少的正規表示式語法。新增的顯著功能包括 自由間距、命名擷取、模式修改器 和 Unicode 類別、區塊和指令碼。它也會將無效的跳脫字元和不存在的反向參照視為錯誤。

XRegExp 也提供自己的 replace() 方法,其替換文字語法已使用 命名反向參照 增強,且沒有跨瀏覽器差異。它也提供完全符合 JavaScript 標準的 split() 方法。

要使用 XRegExp,首先使用 var myre = XRegExp('regex', 'flags') 建立一個正規表示式物件,其中 flags 是字母 g(全域)、i(不分大小寫)、m(錨點 在換行符號處比對)、s(點 比對換行符號)、x(自由間距)和 n(明確擷取)的組合。XRegExp 3 新增 A(星體)旗標,在比對 Unicode 屬性和區塊時,會包含 U+FFFF 之外的 Unicode 字元。ECMAScript 6 旗標 y(黏著)和 u(Unicode)也可以用在原生支援這些旗標的現代瀏覽器中,但如果瀏覽器沒有內建支援這些旗標,就會擲回錯誤。

然後你可以將建立的 XRegExp 執行個體傳遞給各種 XRegExp 方法。請務必按照以下所示進行呼叫,才能獲得完整的 XRegExp 功能。XRegExp 建構函式傳回的物件是原生 JavaScript RegExp 物件。該物件的方法是瀏覽器內建的 RegExp 方法。你可以透過呼叫 XRegExp.install('natives') 來使用 XRegExp 的方法取代內建的 RegExp 方法。這麼做也會影響由一般 RegExp 建構函式或雙斜線正規表示式字面值建立的 RegExp 物件。

XRegExp.test(str, regex, [pos=0], [sticky=false]) 測試 regex 是否可以比對字串的一部分。pos 引數是字串中比對嘗試應開始的從 0 開始的索引。如果你傳遞 true 或 'sticky' 給 sticky 參數,則只會在 pos 嘗試比對。這類似於在其他風格中將 開始嘗試錨點 \G(XRegExp 不支援)新增到 regex 的開頭。

XRegExp.exec(str, regex, [pos=0], [sticky=false]) 的功能與 XRegExp.test() 相同,但會傳回 null 或陣列,而不是 false 或 true。陣列中的索引 0 儲存整體 regex 比對。索引 1 和之後的索引儲存由擷取群組比對的文字(如果有)。如果 regex 有命名擷取群組,則其比對會在 XRegExp 4 及之前版本中作為陣列上的屬性提供。在 XRegExp 5 中,陣列有一個 group 屬性,該屬性會將擷取群組的名稱作為屬性。XRegExp.exec() 不依賴 lastIndex 屬性,因此可以避免與該屬性相關的跨瀏覽器問題。

XRegExp.forEach(str, regex, callback) 可以輕鬆地反覆執行字串中 regex 的所有比對。它會反覆執行所有比對,而不論 global 旗標和 lastIndex 屬性。會使用四個引數呼叫 callback。前兩個是類似 exec() 傳回的陣列,以及比對在字串中開始的索引。最後兩個是 str 和 regex,與你傳遞給 forEach() 的內容完全相同。

XRegExp.replace(str, regex, replacement, [scope]) 傳回一個字串,其中 str 中 regex 的比對已用 replacement 取代。傳遞 'one' 或 'all' 作為 scope 引數,以只取代第一個比對或所有比對。如果你省略 scope 引數,則 regex.global 旗標會決定是否只取代第一個或所有比對。

「XRegExp.replace()」方法使用自己的替換文字語法。它與原生 JavaScript 語法非常相似。它在某種程度上不兼容,會將無法形成有效替換代碼的美元符號視為錯誤。但好處是它消除了所有瀏覽器間的不一致性。 $$ 會插入一個單一的文字美元符號。 $& 和 $0 會插入 整體正規表示式比對。 $` 和 $' 會插入正規表示式比對左右兩側的 主旨字串部分。 $n、$nn、${n} 和 ${nn} 是 編號反向參照,而 ${name} 是 命名反向參照。

如果你傳遞一個函式作為 replacement 參數,則會以三個或更多參數呼叫它。第一個參數是已比對的字串,其中命名的擷取群組可透過該字串上的屬性取得。第二個和後續參數是正規表示式中每個擷取群組比對到的字串(如果有的話)。最後兩個參數是找到比對的字串索引和原始主旨字串。

XRegExp.split(str, regex, [limit]) 是 String.prototype.split 的替代方案。它精確遵循 JavaScript 的字串分割標準,消除了所有瀏覽器間的不一致性和錯誤。

XRegExp Regular Expression Library for JavaScript
  • 简
  • 繁
  • En
About Regular Expressions » Regular Expressions Reference » Replacement Strings Reference » Replacement Strings Reference: Matched Text and Backreferences

Regex Tools
grep
Languages & Libraries
Boost
Delphi
GNU (Linux)
Groovy
Java
JavaScript
.NET
PCRE (C/C++)
PCRE2 (C/C++)
Perl
PHP
POSIX
PowerShell
Python
R
Ruby
std::regex
Tcl
VBScript
Visual Basic 6
wxWidgets
XML Schema
Xojo
XQuery & XPath
XRegExp
Databases
MySQL
Oracle
PostgreSQL
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

XRegExp Regular Expression Library for JavaScript

XRegExp is an open source JavaScript library developed by Steven Levithan. It supports all modern browsers, as well as many older and even ancient browser versions. It can also be used on the server with Node.js. You can download XRegExp at xregexp.com.

Using the XRegExp object instead of JavaScript’s built-in RegExp object provides you with an regular expression syntax with more features and fewer cross-browser inconsistencies. Notable added features include free-spacing, named capture, mode modifiers, and Unicode categories, blocks, and scripts. It also treats invalid escapes and non-existent backreferences as errors.

XRegExp also provides its own replace() method with a replacement text syntax that is enhanced with named backreferences and no cross-browser inconsistencies. It also provides a split() method that is fully compliant with the JavaScript standard.

To use XRegExp, first create a regular expression object with var myre = XRegExp('regex', 'flags') where flags is a combination of the letters g (global), i (case insensitive), m (anchors match at line breaks), s (dot matches line breaks), x (free-spacing), and n (explicit capture). XRegExp 3 adds the A (astral) flag which includes Unicode characters beyond U+FFFF when matching Unicode properties and blocks. The ECMAScript 6 flags y (sticky) and u (Unicode) can also be used in modern browsers that support them natively, but they’ll throw errors in browsers that don’t have built-in support for these flags.

You can then pass the XRegExp instance you constructed to various XRegExp methods. It’s important to make the calls as shown below to get the full XRegExp functionality. The object returned by the XRegExp constructor is a native JavaScript RegExp object. That object’s methods are the browser’s built-in RegExp methods. You can replace the built-in RegExp methods with XRegExp’s methods by calling XRegExp.install('natives'). Doing so also affects RegExp objects constructed by the normal RegExp constructor or double-slashed regex literals.

XRegExp.test(str, regex, [pos=0], [sticky=false]) tests whether the regex can match part of a string. The pos argument is a zero-based index in the string where the match attempt should begin. If you pass true or 'sticky' for the sticky parameter, then the match is only attempted at pos. This is similar to adding the start-of-attempt anchor \G (which XRegExp doesn’t support) to the start of your regex in other flavors.

XRegExp.exec(str, regex, [pos=0], [sticky=false]) does the same as XRegExp.test() but returns null or an array instead of false or true. Index 0 in the array holds the overall regex match. Indexes 1 and beyond hold the text matched by capturing groups, if any. If the regex has named capturing groups then their matches are available as properties on the array in XRegExp 4 and prior. In XRegExp 5 the array has a group property which then has the names of the capturing groups as properties. XRegExp.exec() does not rely on the lastIndex property and thus avoids cross-browser problems with that property.

XRegExp.forEach(str, regex, callback) makes it easy to iterate over all matches of the regex in a string. It always iterates over all matches, regardless of the global flag an the lastIndex property. The callback is called with four arguments. The first two are an array like returned by exec() and the index in the string that the match starts at. The last two are str and regex exactly as you passed them to forEach().

XRegExp.replace(str, regex, replacement, [scope]) returns a string with the matches of regex in str replaced with replacement. Pass 'one' or 'all' as the scope argument to replace only the first match or all matches. If you omit the scope argument then the regex.global flag determines whether only the first or all matches are replaced.

The XRegExp.replace() method uses its own replacement text syntax. It is very similar to the native JavaScript syntax. It is somewhat incompatible by making dollar signs that don’t form valid replacement tokens an error. But the benefit is that it eliminates all cross-browser inconsistencies. $$ inserts a single literal dollar sign. $& and $0 insert the overall regex match. $` and $' insert the part of the subject string to the left and the right of the regex match. $n, $nn, ${n}, and ${nn} are numbered backreferences while ${name} is a named backreference.

If you pass a function as the replacement parameter, then it will be called with three or more arguments. The first argument is the string that was matched, with named capturing groups available through properties on that string. The second and following arguments are the strings matched by each of the capturing groups in the regex, if any. The final two arguments are the index in the string at which the match was found and the original subject string.

XRegExp.split(str, regex, [limit]) is an alternative to String.prototype.split. It accurately follows the JavaScript standard for splitting strings, eliminating all cross-browser inconsistencies and bugs.

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