var country = new RegExp("England");
This could have been defined as well using a direct assignment:
var country = /England/;
Once a regular expression is defined, we can use it to pattern-match and potentially change strings. The following simple example matches a piece of the string in the variable .geographicLocation and substitutes it for another string.
var country = new RegExp("England"); var geographicLocation = "New England"; document.write("Destination for work: "+geographicLocation+"<<br />>"); geographicLocation = geographicLocation.replace(country, "Zealand"); document.write("Destination for vacation: "+geographicLocation);
The result of the execution of this script is shown next.