I have the following string and regex:
var string = "Dear [to name], [your name] has decided to share this [link]";
var patt = /\[+[A-Za-z0-9]+\]/;
I want to be able to change each of the bracketed variables with dynamic input. How would I use match()
or replace()
to target the 1st, 2nd and 3rd occurrences of this regex?
EDIT: At the moment if I do something like document.write(body.match(patt));
it will match only the last one [link]
EDIT: The entire string is take from the value of a text box. The values for each of the brackets are taken from other text inputs and need to be inserted into the string before the text is put back into the text box.
I have the following string and regex:
var string = "Dear [to name], [your name] has decided to share this [link]";
var patt = /\[+[A-Za-z0-9]+\]/;
I want to be able to change each of the bracketed variables with dynamic input. How would I use match()
or replace()
to target the 1st, 2nd and 3rd occurrences of this regex?
EDIT: At the moment if I do something like document.write(body.match(patt));
it will match only the last one [link]
EDIT: The entire string is take from the value of a text box. The values for each of the brackets are taken from other text inputs and need to be inserted into the string before the text is put back into the text box.
Share Improve this question edited Jun 7, 2017 at 19:40 freginold 3,9563 gold badges15 silver badges29 bronze badges asked Aug 4, 2010 at 22:30 kalpaitchkalpaitch 5,27110 gold badges45 silver badges69 bronze badges 4- Out of curiosity, what about just using variables? – hookedonwinter Commented Aug 4, 2010 at 22:32
- The text is being take from a textbox.value – kalpaitch Commented Aug 4, 2010 at 22:34
- Just to clarify - the format string ("Dear [blah] etc [blah]") is taken from a text box, or the values you fill in that string with are taken from a text box? – Merlyn Morgan-Graham Commented Aug 4, 2010 at 22:54
- 1 @Merlyn Morgan-Graham: The entire string is take from the value of the text box. The vales for each of the brackets are taken from other text inputs and need to be inserted into the string before the text is put back into the text box – kalpaitch Commented Aug 4, 2010 at 22:59
2 Answers
Reset to default 19Use a function as the second argument to the replace
method:
var replacement = { "to name": "Joe", "your name": "Fred", "link": "foo" };
string = string.replace(/\[([^\]]+)\]/g, function (_, group) {
return replacement[group];
});
Oh, and the reason your pattern is only matching the [link]
text is because it allows only alphanumeric characters between brackets, not spaces.
EDIT: If the content of the brackets is unimportant, and you just want to replace them in order, use an array instead of a hash:
var replacement = [ "Joe", "Fred", "foo" ];
var index = 0;
string = string.replace(/\[[^\]]+\]/g, function () {
return replacement[index++];
});
Assuming those bracketed items are always the same, you don't have to use regex at all.
var string = "Dear [to name], [your name] has decided to share this [link]";
var name = 'Bob';
var your_name = 'Jacob';
var link = 'http://google.com';
string = string.replace( '[to name]', name ).replace( '[your name]', your_name ).replace( '[link]', link )
alert( string )