RegExp compile() Method - Javascript RegExp

Javascript examples for RegExp:Method

Description

The compile() method compiles a regular expression during execution of a script.

Parameter Values

Parameter Description
regexpA regular expression
modifier Specifies the type of matching.

modifier could be the following:

  • "g" for a global match,
  • "i" for a case-insensitive match
  • "gi" for a global, case-insensitive match

The following code shows how to Do a global search for "man" in a string, and replace it with "person".

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<script>

var str="The following code shows how to man woman person";
var patt=/man/g;//from  w  w  w . ja  v  a2  s  .co m
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>

</body>
</html>

Related Tutorials