lastMatch 屬性 ($&)

從任何規則運算式搜尋傳回最後一個符合的字元。 唯讀。

RegExp.lastMatch

引數

  • RegExp
    必要項。 全域 RegExp 物件。

備註

lastMatch 屬性 (Property) 的初始值是空字串。 每次比對成功時,lastMatch 屬性的值就會變更。

注意事項注意事項

當程式是以 JScript 預設的快速模式執行時,無法使用 RegExp 物件的屬性。 若要從命令提示字元編譯使用這些屬性的程式,必須使用 /fast- 關閉快速選項。 因為執行緒的問題,在 ASP.NET 中關閉快速選項並不安全。

lastMatch 屬性的簡短形式為 $&。 運算式 RegExp["$&"]RegExp.lastMatch 可以替換使用。

範例

以下範例示範 lastMatch 屬性的用法:

// Create the regular expression pattern.
var re = new RegExp("d(b+)(d)","ig");
var str = "cdbBdbsbdbdz";

// Perform the search.
var arr = re.exec(str);

// Create the output.
var s = "" 
s += "$1: " + RegExp.$1 + "\n";
s += "$2: " + RegExp.$2 + "\n";
s += "$3: " + RegExp.$3 + "\n";
s += "input: " + RegExp.input + "\n";
s += "lastMatch: " + RegExp.lastMatch + "\n";
s += "leftContext: " + RegExp.leftContext + "\n";
s += "rightContext: " + RegExp.rightContext + "\n"; 
s += "lastParen: " + RegExp.lastParen + "\n";

這個程式的輸出如下:

$1: bB
$2: d
$3: 
input: cdbBdbsbdbdz
lastMatch: dbBd
leftContext: c
rightContext: bsbdbdz
lastParen: d

需求

版本 5.5

套用至︰

RegExp 物件

請參閱

參考

$1...$9 屬性

index 屬性

input 屬性 ($_)

lastIndex 屬性

lastParen 屬性 ($+)

leftContext 屬性 ($`)

rightContext 屬性 ($')