Instead of using the /regex\d/g
syntax, you can construct a new RegExp object:
var replace = "regex\\d";
var re = new RegExp(replace,"g");
You can dynamically create regex objects this way. Then you will do:
"mystring1".replace(re, "newstring");
More Related Contents:
- What’s the best way to validate an email address in JavaScript?
- How can I use Unicode-aware regular expressions in JavaScript?
- How to use JavaScript regex over multiple lines?
- Including a hyphen in a regex character bracket?
- javascript regex – look behind alternative?
- Named capturing groups in JavaScript regex?
- Javascript Regexp dynamic generation from variables? [duplicate]
- Positive look behind in JavaScript regular expression
- Fastest way to check a string contain another substring in JavaScript?
- Check if a string is html or not
- How to test valid UUID/GUID?
- Capture value out of query string with regex?
- Get URL from background-image Property
- Regular expression for all printable characters in JavaScript
- regex street address match
- regex javascript to match both RGB and RGBA
- jQuery Youtube URL Validation with regex
- Replace function not replacing [duplicate]
- Why can’t I use accented characters next to a word boundary?
- Include Arabic characters in JavaScript regular expression?
- How to convert a Title to a URL slug in jQuery?
- Optimize long lists of fixed string alternatives in regex
- Replace certain arabic words in text string using Javascript
- What does the “Nothing to repeat” error mean when using a regex in javascript?
- Regular expression for number with length of 4, 5 or 6
- Use RegExp to match a parenthetical number then increment it
- What’s different between Python and Javascript regular expressions?
- Extract keyword from Google search in Javascript
- Regex matching emoticons
- Regex: only alphanumeric but not if this is pure numeric