There are two ways to create a regular expression:
1) Using the literal form
var re = /\w+/;
2) Using the object creation form
var re = new RegExp("\\w+");
Usually you need a literal form. In your case, if you create it from a string, you should use the object creation form.
var re = new RegExp("^" + name);
source share