Home > Web Front-end > Front-end Q&A > Is comp a JavaScript method?

Is comp a JavaScript method?

藏色散人
Release: 2023-01-07 11:46:22
Original
1941 people have browsed it

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)
Copy after login

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>
Copy after login

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!
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template