Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create regex from string javascript

I have a question regarding creating a regex from a string. The code is in javascript, basically a variable gets a string. I am not sure how to convert the string to the regex. Here is the code

  var string = "the code";
  var regex = /(the |code )/g;

How can I convert my string to regex using javascript?

like image 270
user2692270 Avatar asked Oct 21 '25 15:10

user2692270


1 Answers

use RegExp:

var stringRe = "the code";
var re = new RegExp(stringRe, "g");
like image 185
taxicala Avatar answered Oct 24 '25 04:10

taxicala