Latest web development tutorials

JavaScript compile () method

RegExp Object Reference JavaScript RegExp Object

Definition and Usage

compile () method is used during the execution of the script compiled regular expression.

compile () method can also be used to change and recompile the regular expression.

grammar

RegExpObject.compile(regexp,modifier)

参数 描述
regexp 正则表达式。
modifier 规定匹配的类型。"g" 用于全局匹配,"i" 用于区分大小写,"gi" 用于全局区分大小写的匹配。


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

In addition to the Opera browser, the other browsers support compile () method.


Examples

Examples

In the global search for the string "man", and replaced with "person". Then compile () method to change the regular expression, use "person" to replace "man" or "woman" ,:

<script>

var str="Every man in the world! Every woman on earth!";
var patt=/man/g;
var str2=str.replace(patt,"person");
document.write(str2+"<br>");
patt=/(wo)?man/g;
patt.compile(patt);
str2=str.replace(patt,"person");
document.write(str2);

</script>

Examples of the above output:

Every person in the world! Every woperson on earth!
Every person in the world! Every person on earth!

try it"


RegExp Object Reference JavaScript RegExp Object