How to replace all in string

Hello All,

Please Guide me how to replace all in string

i want to replace rep with " ".
getting the value of id is dynamically from payload.

var rep = "ID"+" "+id+"_";

msg.payload=s.replace(rep," ");

This is working for single message. when multiple message is getting it replace only 1st message i want to replace all messages.

please guide me how to replace all string.

thanks

You mean like this ? Replacing multiple instances of a string inside another string in JavaScript | Building SPAs

I tried this also but not working in my condition

var rep = "UI"+""+UnitId+"_";

msg.payload=s.replace(/rep/g,' ')

var text = "UI" + "" + UnitId + "_";

msg.payload = text.replace(/rep/g,' ')

You would need to create the the regex pattern using the constructor property

var patt = new RegExp(rep, "g");
  msg.payload = s.replace(patt, " ");

https://www.w3schools.com/jsref/jsref_regexp_constructor.asp

1 Like

its working for me... Thank you

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.