Javascript examples for RegExp:Method
The compile() method compiles a regular expression during execution of a script.
Parameter | Description |
---|---|
regexp | A regular expression |
modifier | Specifies the type of matching. |
modifier could be the following:
The following code shows how to Do a global search for "man" in a string, and replace it with "person".
<!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>