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. Quote 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. Quote Link to comment https://forums.phpfreaks.com/topic/289899-dynamic-value-in-pattern/#findComment-1485504 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.