0

suppose i have string

'bold':'','Dateformat':'mm/dd/yyyy','Lowercase':''

or

'bold':'','Lowercase':'','Dateformat':'mm/dd/yyyy'

or

'Dateformat':'mm/dd/yyyy' 

can be in any place within comma seperated string

and i want to replace

'Dateformat':'mm/dd/yyyy' with blank

and

mm/dd/yyyy

can be any string (dynamic value)

please tell me how to do that. Advance thanks for help.

Rikhi Sahu
  • 655
  • 1
  • 7
  • 19

2 Answers2

2

Is this you wanted??

var str = "'bold':'','Dateformat':'mm/dd/yyyy','Lowercase':''";

var str_to_replace = "'Dateformat':'mm/dd/yyyy'";
var new_str = str.replace(str_to_replace,'');


console.log(new_str);
Dinesh undefined
  • 5,490
  • 2
  • 19
  • 40
2

You can create a Javascript object from the string and make the value of the key ""

var string = "'bold':'','Dateformat':'mm/dd/yyyy','Lowercase':''";
var obj = JSON.parse('{' + string.replace(/'/g, '"') + '}');
var obj2 = JSON.parse('{' + string.replace(/'/g, '"') + '}');

if (obj.Dateformat) {
  obj.Dateformat = '';
}

string = JSON.stringify(obj).replace("{", '').replace("}", '');

console.log(string);

//or delete the whole key

if (obj2.Dateformat) {
  delete obj2.Dateformat;
}

string = JSON.stringify(obj2).replace("{", '').replace("}", '');

console.log(string);
sabithpocker
  • 15,274
  • 1
  • 42
  • 75
  • @sabithpoker your code is good, but can you please tell me i am trying to use variable with obj like obj. but it looks for variable name insteadof value. – Rikhi Sahu May 29 '17 at 06:03
  • just ignore that, your code was helpful for me so i did accepted your answer. Thanks – Rikhi Sahu May 30 '17 at 13:04