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");
Related Contents:
- Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters
- How do I split a string with multiple separators in JavaScript?
- What is a good regular expression to match a URL? [duplicate]
- How to replace plain URLs with links?
- How to use JavaScript regex over multiple lines?
- Get Substring between two characters using javascript
- How to make Regular expression into non-greedy?
- Convert camelCaseText to Title Case Text
- Detect URLs in text with JavaScript
- How to convert a Title to a URL slug in jQuery?
- Javascript and regex: split string and keep the separator
- How to extract base URL from a string in JavaScript?
- How can I concatenate regex literals in JavaScript?
- Negative lookbehind equivalent in JavaScript
- Use dynamic (variable) string as regex pattern in JavaScript
- Create RegExps on the fly using string variables
- Regular Expression to reformat a US phone number in Javascript
- JavaScript replace/regex
- Javascript regex returning true.. then false.. then true.. etc [duplicate]
- Check if a string is html or not
- Works in Chrome, but breaks in Safari: Invalid regular expression: invalid group specifier name /(?
- Count number of matches of a regex in Javascript
- How can I parse a CSV string with JavaScript, which contains comma in data?
- Fastest method to escape HTML tags as HTML entities?
- What’s the meaning of /gi in a regex? [duplicate]
- Escape dot in a regex range
- How to capture an arbitrary number of groups in JavaScript Regexp?
- JS regex to split by line
- Including a hyphen in a regex character bracket?
- Regex to extract substring, returning 2 results for some reason
- jQuery javascript regex Replace with \n
- javascript – replace dash (hyphen) with a space
- Javascript – return string between square brackets
- Remove trailing character(s) from string in Javascript
- How to put variable in regular expression match?
- How to loop all the elements that match the regex?
- Negative lookahead Regular Expression
- Converting Date and Time To Unix Timestamp
- How can I select an element by ID with jQuery using regex?
- converting RegExp to String then back to RegExp
- Matching special characters and letters in regex
- JavaScript Regex Global Match Groups
- Regex exec only returning first match [duplicate]
- Dedicated Regular Expression for Persian alphabet [duplicate]
- Extract parameter value from url using regular expressions
- Regular expression for number with length of 4, 5 or 6
- regular expression to match exactly 5 digits
- Convert dash-separated string to camelCase?
- Regular Expression for alphabets with spaces
- What is the need for caret (^) and dollar symbol ($) in regular expression?