Posted By

noah on 05/27/09


Tagged

regex javascript filter match regular Expression regulareexpression


Versions (?)

Use a variable in a JavaScript regular expression


 / Published in: JavaScript
 

Use the RegExp constructor to create the regular expression. You can pass RegExp any string as an argument and it will be converted to a regex.

  1. var baz = "foo";
  2.  
  3. var filter = new RegExp(baz + "d")
  4.  
  5. "food fight".match(filter);
  6.  
  7. // returns ["food"]

Report this snippet  

You need to login to post a comment.