sandy1028 Posted July 15, 2014 Share Posted July 15, 2014 How to make "hints" as dynamic varaible: var myRegExp = /hints\s=\s'(.+?)';/; I am trying to use as var myRegExp = "/"+hints+/\s=\s'(.+?)';/; which doesnt work. Link to comment https://forums.phpfreaks.com/topic/289899-dynamic-value-in-pattern/ Share on other sites More sharing options...
.josh Posted July 17, 2014 Share Posted July 17, 2014 You can't use variables in a regex literal. You need to create a new RexExp object to do it. Example: var hints = 'foobar'; var myRegExp = new RegExp(hints+"\\s=\\s'(.+?)';"); console.log(myRegExp); // output: /foobar\s=\s'(.+?)';/ Also note the necessity to double escape backslashes. Link to comment https://forums.phpfreaks.com/topic/289899-dynamic-value-in-pattern/#findComment-1485504 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.