0

I have a message like this:

"Getting Closer! Only $:is_left_to_reach_cart_total_goal away from FREE SHIPPING!"

I need to replace :is_left_to_reach_cart_total_goal with actual number. Also message can contain others variables like :cart_total_currently , :cart_total_goal , :cart_products_quantity_currently , :cart_products_quantity_goal , :is_left_to_reach_products_quantity_goal , :link , :button .

Any suggestions?

2
  • If you don't have a lot of such special strings, just loop over them and use replace. Commented Dec 22, 2015 at 9:11
  • You should create array for the words you want to replace, then do foreach of those word str.replace(word,number) Commented Dec 22, 2015 at 9:13

3 Answers 3

1

This replaces your variable with '2'.

var str = "Getting Closer! Only $:is_left_to_reach_cart_total_goal away from FREE SHIPPING!";
var res = str.replace("$:is_left_to_reach_cart_total_goal", "2");

JavaScript String replace() Method

Sign up to request clarification or add additional context in comments.

Comments

0

You can have an object which will have keys that can appear in string and hold their values and replace it using loop. Following is an example:

JSFiddle

function splitStr(str, seperator) {
  return str.split(seperator)
}

function main() {
  var str = "Getting Closer! Only $:cart_total_currently , $:cart_total_goal , $:cart_products_quantity_currently , $:cart_products_quantity_goal , $:is_left_to_reach_products_quantity_goal , $:link , $:button away from FREE SHIPPING!";

  var valueObj = {
    "cart_total_currently": 100,
    "cart_total_goal": 200,
    "cart_products_quantity_currently": 2,
    "cart_products_quantity_goal": 5,
    "is_left_to_reach_products_quantity_goal": 3,
    "link": "www.google.com",
    "button": "<a href='www.google.com'>link</a>"
  }

  var arrStr = splitStr(str, '$:');
  var resultStr = replaceStr(arrStr, valueObj);
  console.log(resultStr);
}

function replaceStr(arr, obj) {
	var retStr = "";
	arr.forEach(function(row){
  	var key = row.split(" ")[0];
    if(obj.hasOwnProperty(key)){
    	retStr += row.replace(key,obj[key]);
    }
    else{
    	retStr += row;
    }
  });
  return retStr;
}
main();

Comments

0

This will replace your original string with number:

var original = "Getting Closer! Only $:is_left_to_reach_cart_total_goal away from FREE SHIPPING!";
var number = 1;
var newone = original.replace(/:is_left_to_reach_cart_total_goal/g, number);

you can also create an array of placeholders and values you want to replace and then reduce it like this:

var original = "Getting Closer!"
             + "Only $:is_left_to_reach_cart_total_goal away from FREE SHIPPING!"
             + "Additional info: $:link";

var toReplace = [
    { key: ":is_left_to_reach_cart_total_goal", value: "1" },
    { key: ":cart_total_currently", value: "2" },
    { key: ":cart_total_goal", value: "3" },
    { key: ":cart_products_quantity_currently", value: "4" },
    { key: ":cart_products_quantity_goal", value: "5" },
    { key: ":is_left_to_reach_products_quantity_goal", value: "6" },
    { key: ":link", value: "7" },
    { key: ":button", value: "whateverelse" }
];

var result = toReplace.reduce(function(r, item) {
    return r.replace(new RegExp(item.key, "g"), item.value);
}, original);
console.log(result);

NOTICE: using g flag to replace all occurences of string.

Comments

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.