Javascript compile () method, javascriptcompile
Usage
RegExpObject. compile (regexp, modifier)
It is used to compile regular expressions during script execution, or to change and recompile regular expressions.
Regexp is a regular expression.
Modifier is the type of the specified match. "G" is used for global matching, "I" is used for case-sensitive, and "gi" is used for global case-sensitive matching.
Example:
Search for "today" globally in the string and replace it with "tommorow.
Then, use the compile () method to change the Regular Expression and replace "rld" or "world" with "boat ",:
<Html> <body> <script type = "text/javascript"> var str = "hello world, today is a good day! "; Patt =/today/g; str2 = str. replace (patt, "tommorow"); document. write (str2 + "<br/>"); patt =/(wo )? Rld/g; patt. compile (patt); str2 = str. replace (patt, "boat"); document. write (str2); </script> </body>