multiline 屬性
傳回布林值,該布林值表示規則運算式所使用之 multiline 旗標 (m) 的狀態。
rgExp.multiline
引數
- rgExp
必要項。 規則運算式物件的執行個體。
備註
multiline 屬性是唯讀的,如果已設定規則運算式的 multiline 旗標,就會傳回 true,否則會傳回 false。 如果是以 m 旗標建立規則運算式物件,multiline 屬性為 true。 預設值為 false。
如果 multiline 是 false,「^」會比對字串的開始位置,而「$」則會比對字串的結束位置。 如果 multiline 是 true,「^」會比對字串的開始位置,以及「\n」或「\r」之後的位置;而「$」則會比對字串的結束位置,以及「\n」或「\r」之前的位置。
範例
下列範例示範 multiline 屬性的行為。 如果您將 m 傳入以下顯示的函式,則會將單字 "while" 取代成 "and" 一字。 這是因為您設定了 multiline 旗標,而且單字 "while" 出現在新行字元後面的行首。 multiline 旗標可讓您對多行字串執行搜尋作業。
function RegExpMultilineDemo(flag){
// The flag parameter is a string that contains
// g, i, or m. The flags can be combined.
// Check flags for validity.
if (flag.match(/[^gim]/))
{
return ("Flag specified is not valid");
}
// Create the string on which to perform the replacement.
var ss = "The man hit the ball with the bat ";
ss += "\nwhile the fielder caught the ball with the glove.";
// Replace "while" with "and".
var re = new RegExp("^while", flag);
var r = ss.replace(re, "and");
// Output the multiline flag and the resulting string.
var s = "";
s += "Result for multiline = " + re.multiline.toString();
s += ": " + r;
return(s);
}
print (RegExpMultilineDemo("m"));
print (RegExpMultilineDemo(""));