Let's step through the code to understand what is happening. We need to get a regex that looks like this: b(wordToMatch1|wordToMatch2|wordToMatch3)b.
What this basically says is "find me any of the words and only whole words which are denoted by b". When we look at the list we created, we will see the words we want to remove from the input string:
We then created a simple loop that will create the list of words to match using the OR metacharacter. We ended up with a BadWord1|BadWord2|BadWord3 pattern after the foreach loop has completed. However, this is still not a valid regex:
To complete the pattern resulting in the valid ...