JavaScript compile() Method
Complete RegExp Object Reference
Definition and Usage
The compile() method is used to change the regular expression.
Syntax
RegExpObject.compile(regexp)
|
Parameter |
Description |
regexp |
Required. The new regular expression to search for |
Example
In the following example we change the pattern from "Microsoft"
to "W3Schools", and then test if we find the specified pattern in a
string:
<script type="text/javascript">
var str="Visit W3Schools";
var patt=new RegExp("Microsoft");
document.write("Test if we find " + patt + " in " + str + ": ")
if (patt.test(str)==true)
{
document.write("Match found!")
}
else
{
document.write("Match not found")
}
patt.compile("W3Schools");
document.write("<br />")
document.write("Test if we find " + patt + " in " + str + ": ")
if (patt.test(str)==true)
{
document.write("Match found!")
}
else
{
document.write("Match not found")
}
</script>
|
The output of the code above will be:
Test if we find /Microsoft/ in Visit W3Schools: Match not found
Test if we find /W3Schools/ in Visit W3Schools: Match found!
|
Try-It-Yourself Demos
compile()
and test()
How to use compile() to change the regular expression and test() to test if we
find a specified pattern in a string.
Complete RegExp Object Reference
|
|
|
See why there are 20,000+ Ektron integrations worldwide.
Request an INSTANT DEMO or download a FREE TRIAL today. |
|
|
|