語言

Regex.Escape(String) 方法

定義

透過將最小字元集合(如 、*、+、?、|、{、[, (, ), ^、$、.、#、以及空白位)來轉義,方法是將它們替換成轉義碼。 這會指示正則表達式引擎以字面方式解譯這些字元,而不是中繼字元。

public:
 static System::String ^ Escape(System::String ^ str);
public static string Escape(string str);
static member Escape : string -> string
Public Shared Function Escape (str As String) As String

參數

str
String

包含要轉換文字的輸入字串。

傳回

一串帶有元角色轉換成逃脫形態的角色。

例外狀況

str 是 null。

備註

Escape(String) 將字串轉換成正則表達式引擎,將可能包含的任何元字元解讀為字元字面值。 對於使用設計時未知字元動態定義的字串,這尤其重要。

此方法跳脫直開括號([)與開括號({)字元,但逃逸不避開對應的閉尾字元(]和})。 大多數情況下,逃避這些並非必要。

適用於

另請參閱