First, the answer to the question:
The other answers are nearly correct, but fail to consider what happens when the text to be matched contains a literal backslash, (i.e. when: regexp_part
contains a literal backslash). For example, what happens when regexp_part
equals: "C:Windows"
? In this case the suggested methods do not work as expected (The resulting regex becomes: /C:Windows/
where the W
is erroneously interpreted as a non-word character class). The correct solution is to first escape any backslashes in regexp_part
(the needed regex is actually: /C:\Windows/
).
To illustrate the correct way of handling this, here is a function which takes a passed phrase and creates a regex with the phrase wrapped in
word boundaries:
// Given a phrase, create a RegExp object with word boundaries.
function makeRegExp(phrase) {
// First escape any backslashes in the phrase string.
// i.e. replace each backslash with two backslashes.
phrase = phrase.replace(/\/g, "");
// Wrap the escaped phrase with word boundaries.
var re_str = "\b"+ phrase +"\b";
// Create a new regex object with "g" and "i" flags set.
var re = new RegExp(re_str, "gi");
return re;
}
// Here is a condensed version of same function.
function makeRegExpShort(phrase) {
return new RegExp("\b"+ phrase.replace(/\/g, "") +"\b", "gi");
}
To understand this in more depth, follows is a discussion...
In-depth discussion, or "What's up with all these backslashes!?"
JavaScript has two ways to create a RegExp object:
- /pattern/flags - You can specify a RegExp Literal expression directly, where the pattern is delimited using a pair of forward slashes followed by any combination of the three pattern modifier flags: i.e.
'g'
global, 'i'
ignore-case, or 'm'
multi-line. This type of regex cannot be created dynamically.
- new RegExp("pattern", "flags") - You can create a RegExp object by calling the
RegExp()
constructor function and pass the pattern as a string (without forward slash delimiters) as the first parameter and the optional pattern modifier flags (also as a string) as the second (optional) parameter. This type of regex can be created dynamically.
The following example demonstrates creating a simple RegExp object using both of these two methods. Lets say we wish to match the word "apple"
. The regex pattern we need is simply: apple
. Additionally, we wish to set all three modifier flags.
Example 1: Simple pattern having no special characters: apple
// A RegExp literal to match "apple" with all three flags set:
var re1 = /apple/gim;
// Create the same object using RegExp() constructor:
var re2 = new RegExp("apple", "gim");
Simple enough. However, there are significant differences between these two methods with regard to the handling of escaped characters. The regex literal syntax is quite handy because you only need to escape forward slashes - all other characters are passed directly to the regex engine unaltered. However, when using the RegExp constructor method, you pass the pattern as a string, and there are two levels of escaping to be considered; first is the interpretation of the string and the second is the interpretation of the regex engine. Several examples will illustrate these differences.
First lets consider a pattern which contains a single literal forward slash. Let's say we wish to match the text sequence: "and/or"
in a case-insensitive manner. The needed pattern is: and/or
.
Example 2: Pattern having one forward slash: and/or
// A RegExp literal to match "and/or":
var re3 = /and/or/i;
// Create the same object using RegExp() :
var re4 = new RegExp("and/or", "i");
Note that with the regex literal syntax, the forward slash must be escaped (preceded with a single backslash) because with a regex literal, the forward slash has special meaning (it is a special metacharacter which is used to delimit the pattern). On the other hand, with the RegExp constructor syntax (which uses a string to store the pattern), the forward slash does NOT have any special meaning and does NOT need to be escaped.
Next lets consider a pattern which includes a special:
word boundary regex metasequence. Say we wish to create a regex to match the word "apple"
as a whole word only (so that it won't match "pineapple"
). The pattern (as seen by the regex engine) needs to be: apple
:
Example 3: Pattern having
word boundaries: apple
// A RegExp literal to match the whole word "apple":
var re5 = /apple/;
// Create the same object using RegExp() constructor:
var re6 = new RegExp("\bapple\b");
In this case the backslash must be escaped when using the RegExp constructor method, because the pattern is stored in a string, and to get a literal backslash into a string, it must be escaped with another backslash. However, with a regex literal, there is no need to escape the backslash. (Remember that with a regex literal, the only special metacharacter is the forward slash.)
Backslash SOUP!
Things get even more interesting when we need to match a literal backslash. Let's say we want to match the text sequence: "C:Program FilesJGsoftRegexBuddy3RegexBuddy.exe"
. The pattern to be processed by the regex engine needs to be: C:\Program Files\JGsoft\RegexBuddy3\RegexBuddy.exe
. (Note that the regex pattern to match a single backslash is \
i.e. each must be escaped.) Here is how you create the needed RegExp object using the two JavaScript syntaxes
Example 4: Pattern to match literal back slashes:
// A RegExp literal to match the ultimate Windows regex debugger app:
var re7 = /C:\Program Files\JGsoft\RegexBuddy3\RegexBuddy.exe/;
// Create the same object using RegExp() constructor:
var re8 = new RegExp(
"C:\\Program Files\\JGsoft\\RegexBuddy3\\RegexBuddy\.exe");
This is why the /regex literal/
syntax is generally preferred over the new RegExp("pattern", "flags")
method - it completely avoids the backslash soup that can frequently arise. However, when you need to dynamically create a regex, as the OP needs to here, you are forced to use the new RegExp()
syntax and deal with the backslash soup. (Its really not that bad once you get your head wrapped 'round it.)
RegexBuddy to the rescue!
RegexBuddy is a Windows app that can help with this backslash soup problem - it understands the regex syntaxes and escaping requirements of many languages and will automatically add and remove backslashes as required when pasting to and from the application. Inside the application you compose and debug the regex in native regex format. Once the regex works correctly, you export it using one of the many "copy as..." options to get the needed syntax. Very handy!