You are mixing two ways to create regular expressions in JavaScript. If you use a regular expression literal, / is a regular expression delimiter, the g modifier immediately follows the closing delimiter, and \b is the escape sequence for the word boundary:
var regex = /width\b/g;
If you create it as a string literal for the RegExp constructor, you will leave the regular expression delimiters, you will pass the modifiers in the form of the second string argument, and you will have to double the backslash in the regular expression escape sequences:
var regex = new RegExp('width\\b', 'g');
As you do this, \b converted to a backspace character before it reaches the regular expression compiler; you need to avoid the backslash in order to get it after processing the JavaScript literal escape string string. Or use a regex literal.
Alan moore
source share