# Trying to create a function with replace and variables

**URL:** https://discourse.nodered.org/t/trying-to-create-a-function-with-replace-and-variables/34861
**Category:** General
**Created:** [26 October 2020 21:50 UTC](https://discourse.nodered.org/t/trying-to-create-a-function-with-replace-and-variables/34861 "2020-10-26T21:50:09Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![RogierQ](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rogierq/32/3211_2.png) [@RogierQ](https://discourse.nodered.org/u/RogierQ)
#### Post date: [26 October 2020 21:50 UTC](https://discourse.nodered.org/t/trying-to-create-a-function-with-replace-and-variables/34861/1 "2020-10-26T21:50:09Z")

</div>

Last frustration for tonight i hope 😉

i created the function below

```auto
function findReplace(find,replace,html) {
    html = html.replace( find/g, replace ); //(/blue/gi, "red");
    return html;
}

```

where find is a variable  
i want to change every time the variable is found  
normally with just one word this works with `(/blue/g, "red");`  
but when i replace blue with find the variable becomes fixed... mwaaa  
i guess i have to do something with // \ /\ / but no matter what i try i get errors and in google i use or the wrong search tag or there is no answer...

How does this needs to be done?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [26 October 2020 22:44 UTC](https://discourse.nodered.org/t/trying-to-create-a-function-with-replace-and-variables/34861/2 "2020-10-26T22:44:38Z")

</div>

This should help...

> **[String.prototype.replaceAll()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll)**
>
> The replaceAll() method returns a new string with all matches of a pattern replaced by a replacement.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [26 October 2020 22:50 UTC](https://discourse.nodered.org/t/trying-to-create-a-function-with-replace-and-variables/34861/3 "2020-10-26T22:50:32Z")

</div>

Actually, I don't thing replaceAll is in node yet

Try this...

```auto
function replaceAll(str, find, replace) {
  return str.replace(new RegExp(find, 'g'), replace);
}

```

---

<div class="post-metadata">

### Author: ![RogierQ](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rogierq/32/3211_2.png) [@RogierQ](https://discourse.nodered.org/u/RogierQ)
#### Post date: [26 October 2020 22:50 UTC](https://discourse.nodered.org/t/trying-to-create-a-function-with-replace-and-variables/34861/4 "2020-10-26T22:50:42Z")

</div>

Thanks!  
I tried that but this gives me this error

```auto
"TypeError: payload.replaceAll is not a function"

```

tried replaceAll() in other projects but allways an error

---

<div class="post-metadata">

### Author: ![RogierQ](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rogierq/32/3211_2.png) [@RogierQ](https://discourse.nodered.org/u/RogierQ)
#### Post date: [26 October 2020 22:51 UTC](https://discourse.nodered.org/t/trying-to-create-a-function-with-replace-and-variables/34861/5 "2020-10-26T22:51:11Z")

</div>

you are right, thanks i try that!

---

<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: [25 December 2020 22:51 UTC](https://discourse.nodered.org/t/trying-to-create-a-function-with-replace-and-variables/34861/6 "2020-12-25T22:51:14Z")

</div>

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