Reputation: 3
Here is a fiddle http://jsfiddle.net/aLr2yx8d/
$('#inputButton').click(function() {
var value = $('#input').val();
var re = /\./g;
var output = $('#output');
var text = output.text().replace(re, value);
output.html(text);
});
Now I can only update it once. I want to have the possibility to update it more than once. The first time it will replace the dot (.) and the second time it has to replace the first value I gave.
Upvotes: 0
Views: 78
Reputation: 782
You can use a variable to hold the variable RegularExpression you wish to change. For example:
var regExVal= /\./g;
$('#inputButton').click(function() {
var value = $('#input').val();
var re = regExVal;
regExVal= new RegExp(value, "g"); <-- this is how you set the new value
var output = $('#output');
var text = output.text().replace(re, value);
output.html(text);
});
Edit:
As I mentioned in the comment, you would need to escape certain characters for use as characters in the Regular Expression instead of operators. I found a good answer to that in another question. Include it in your script:
function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}
Then, before using the value
, you do something like:
value = escapeRegExp(value);
Upvotes: 1