# How to replace all in string

**URL:** <https://discourse.nodered.org/t/how-to-replace-all-in-string/39320>\
**Category:** General\
**Created:** [18 January 2021 09:47 UTC](https://discourse.nodered.org/t/how-to-replace-all-in-string/39320 "2021-01-18T09:47:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ptwtech](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@Ptwtech](https://discourse.nodered.org/u/Ptwtech)\
**Post date:** [18 January 2021 09:47 UTC](https://discourse.nodered.org/t/how-to-replace-all-in-string/39320/1 "2021-01-18T09:47:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [18 January 2021 09:58 UTC](https://discourse.nodered.org/t/how-to-replace-all-in-string/39320/2 "2021-01-18T09:58:32Z")

</div>

You mean like this ? [Replacing multiple instances of a string inside another string in JavaScript | Building SPAs](https://www.carlrippon.com/replacing-mutliple-instances-of-a-string-inside-string/)

---

<div class="post-metadata">

**Author:** ![Ptwtech](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@Ptwtech](https://discourse.nodered.org/u/Ptwtech)\
**Post date:** [18 January 2021 11:07 UTC](https://discourse.nodered.org/t/how-to-replace-all-in-string/39320/3 "2021-01-18T11:07:14Z")

</div>

I tried this also but not working in my condition

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

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

---

<div class="post-metadata">

**Author:** ![softy2k](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/softy2k/32/23707_2.png) [@softy2k](https://discourse.nodered.org/u/softy2k)\
**Post date:** [18 January 2021 11:20 UTC](https://discourse.nodered.org/t/how-to-replace-all-in-string/39320/4 "2021-01-18T11:20:32Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [18 January 2021 11:23 UTC](https://discourse.nodered.org/t/how-to-replace-all-in-string/39320/5 "2021-01-18T11:23:04Z")

</div>

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

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

```

[https://www.w3schools.com/jsref/jsref\_regexp\_constructor.asp](https://www.w3schools.com/jsref/jsref_regexp_constructor.asp)

---

<div class="post-metadata">

**Author:** ![Ptwtech](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@Ptwtech](https://discourse.nodered.org/u/Ptwtech)\
**Post date:** [18 January 2021 12:22 UTC](https://discourse.nodered.org/t/how-to-replace-all-in-string/39320/6 "2021-01-18T12:22:25Z")

</div>

its working for me... Thank you

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [1 February 2021 12:22 UTC](https://discourse.nodered.org/t/how-to-replace-all-in-string/39320/7 "2021-02-01T12:22:37Z")

</div>

Closed on 1 February 2021 12:22 UTC

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