Home > Article > Web Front-end > Is comp a JavaScript method?
comp is not a JavaScript method, but compile is a JavaScript method; the compile method is used to compile regular expressions during script execution, and can also be used to change and recompile regular expressions.
The operating environment of this article: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.
Is comp a JavaScript method?
comp is not a JavaScript method, but compile() is a JavaScript method.
JavaScript compile() method
compile() method is used to compile regular expressions during script execution.
The compile() method can also be used to change and recompile regular expressions.
Syntax
RegExpObject.compile(regexp,modifier)
Parameter regexp regular expression.
modifier specifies the matching type. "g" is used for global matching, "i" is used for case-sensitive matching, and "gi" is used for global case-sensitive matching.
Note: Except for the Opera browser, other browsers support the compile() method.
Example
Search globally for "man" in the string and replace it with "person". Then use the compile() method to change the regular expression and replace "man" or "woman" with "person":
<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>
The output result of the above example:
Every person in the world! Every woperson on earth! Every person in the world! Every person on earth!
Recommended learning: "javascript basic tutorial》
The above is the detailed content of Is comp a JavaScript method?. For more information, please follow other related articles on the PHP Chinese website!