Home  >  Article  >  Web Front-end  >  Is comp a JavaScript method?

Is comp a JavaScript method?

藏色散人
藏色散人Original
2021-09-01 15:12:531834browse

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.

Is comp a JavaScript method?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn