JavaScript – Use Variables with Regular Expressions
So you know the basics of Regular Expressions in JavaScript and often use it for replacing text etc. Example:
[code]
var sentence = 'Regular Expression';
sentence.replace(/ssion/, 'shun');
// "Regular Expreshun"
[/code]
Now what if the string you need to replace needs to be flexible? Well you could use a variable!
[code]
var replacee = 'sion';
var replacer = 'hun';
sentence.replace(/+ replacee +/, replacer);
// SyntaxError ...