0

I want to replace some strings and variables but I don`t know how to make it - my code is below:

var text = "I was born in $city in country $country "

var city = "New YORK"
var country = "USA"

var regex_variable = /\$\s*(.*?)\s/g;
var variable =[];

while (c = regex_variable.exec(text)) {
  variable.push(c[1]);
}

for (n=0;n<variable.length;n++){
  text = text.replace(regex_variable, "kat");
}

console.log(text)

Output from this script is:

I was born in katin country kat

But the point is to replace the kat with the strings from variable city and country. Please note that in the var text some strings has got $ character as a prefix - this strings I want to take from the variables (strings in the text will be the same as a names of variables but with prefix $).

The correct output should be:

I was born in New YORK in country USA

Anyone could help me with it?

4
  • 1
    You may want to look at JS template literals. var city = 'New York', country = 'USA', text = `I was born in ${city} in country ${country}`; Commented Sep 1, 2016 at 16:22
  • the point is that I need to use the $city becase this is just an example - I will received this string from the file with variables like $city Commented Sep 1, 2016 at 16:23
  • @RobM. Nice, but not fully compatible. Commented Sep 1, 2016 at 16:24
  • FYI it's "JavaScript", not "java script". Commented Sep 1, 2016 at 16:43

4 Answers 4

2

You can just do it like this:

var text = "I was born in $city in country $country ";

var city = "New YORK";
var country = "USA";

text = text.replace("$city", city);
text = text.replace("$country", country);

console.log(text)
Sign up to request clarification or add additional context in comments.

2 Comments

It`s a simples way - you are right :D but problem will be when I will have got more than 100 of this variables - to make a replace for each variable :)
Do you know the order of the variables, for example that $city comes first and $country is the secound and so on?
1

What you're trying to build is a little "templating" system. If you search for that, you will find lots of things.

Here's a real simple example:

var city = "New YORK";
var country = "USA";
var interpolations = {city, country};
var text = "I was born in $city in country $country";

var newString = text.replace(/\$(\w+)/g, (match, val) => interpolations[val]);

console.log(newString);

1 Comment

Thank you - this what I was looking for ;)
0
var text = "I was born in $city in country $country "

var city = "New YORK"
var country = "USA"

var replaceArray = [city,country]; //create an array with your variables

var regex_variable = /\$\s*(.*?)\s/g;
var variable =[];

while (c = regex_variable.exec(text)) {
   variable.push(c[1]);
}

for (n=0;n<variable.length;n++){
  text = text.replace("$"+variable[n], replaceArray[n]);
}

console.log(text);

If you know the order of the variables you want to replace, this could be your solution.

Comments

0

var city = "NEW YORK"

var country = "USA"

var text = "I was born in city in country "

var res = text.replace(/city/gi, city);

var res1 = res.replace(/country/gi, country);

document.write(res1);

1 Comment

This is a fine solution, but it's not scalable to many variable names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.