multiline 属性 (Regular Expression)
返回布尔值,该值指示在正则表达式中使用multiline标志(m)的状态。默认值为false。只读。
语法
rgExp.multiline
备注
必需的rgExp参数是RegExp对象的实例。
multiline属性在multiline标志为正则表达式设置时,返回true;否则返回false。如果使用m标志创建正则表达式对象,那么multiline属性就是true。
如果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); } sa = RegExpMultilineDemo("m"); sb = RegExpMultilineDemo(""); document.write (sa + "<br />" + sb);
必备条件
在以下文档模式中受支持:怪异模式、Internet Explorer 6标准模式、Internet Explorer 7标准模式、Internet Explorer 8标准模式、Internet Explorer 9标准模式、Internet Explorer 10标准模式、Internet Explorer 11标准模式。应用商店应用(Windows 8和Windows Phone 8.1)中也受支持。请参阅版本信息。
Applies To: Regular Expression Object