<html>
<body>
<script language=JavaScript>
var myRegExp = /(sugar )?candy|choc(olate|oholic)?/gi
var myString = "Mmm, I love chocolate, I'm a chocoholic. " +
"I love candy too, sweet, sugar candy";
myString = myString.replace(myRegExp,"salad");
alert(myString)
</script>
</body>
</html>
Save this as ch08_q3.htm.
For our example, we'll pretend we're creating script for a board on a dieting site where text relating to candy is barred and will be replaced with a much healthier option, salad.
My barred words are
chocolate
choc
chocoholic
sugar candy
candy
Let's see how I built up the regular expression to remove the offending words.
I started with the two basic words, so to match "choc" or "candy," I use
candy|choc
Next I added the matching for "sugar candy." Since the "sugar" bit is optional, we group it by placing it in parentheses and adding the "?" after it. This means match the group zero times or one time.
(sugar )?candy|choc
Finally we need to add the optional "olate" and "oholic" end bits. We add these as a group after the "choc" word and again make the group optional. We can match either of the endings in the group by using the | character.
(sugar )?candy|choc(olate|oholic)?/gi
Finally, we declare it as
var myRegExp = /(sugar )?candy|choc(olate|oholic)?/gi
The gi at the end means the regular expression will find and replace words on a global, case-insensitive basis.
So, to sum up
/(sugar )?candy|choc(olate|oholic)?/gi
reads as:
Either match zero or one occurrences of "sugar" followed by "candy." Or alternatively match "choc" followed by either one or zero occurrences of "olate" or match "choc" followed by zero or one occurrence of "oholic."
Finally, the following:
myString = myString.replace(myRegExp,"salad");
replaces the offending words with "salad" and sets myString to the new clean version:
"Mmm, I love salad, I'm a salad. I love salad too, sweet, salad."
|