# Filter certain text from string

**URL:** <https://discourse.nodered.org/t/filter-certain-text-from-string/55306>\
**Category:** General\
**Created:** [16 December 2021 17:23 UTC](https://discourse.nodered.org/t/filter-certain-text-from-string/55306 "2021-12-16T17:23:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigmac5753](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bigmac5753/32/15733_2.png) [@bigmac5753](https://discourse.nodered.org/u/bigmac5753)\
**Post date:** [16 December 2021 17:23 UTC](https://discourse.nodered.org/t/filter-certain-text-from-string/55306/1 "2021-12-16T17:23:40Z")

</div>

I have this payload that I want to filter:

```auto
Wallet height: 760264
Sync status: Synced
Balances, fingerprint: 3007886911
Wallet ID 1 type STANDARD_WALLET
   -Total Balance: 2958.5843702 xcc (295858437020 mojo)
   -Pending Total Balance: 2958.5843702 xcc (295858437020 mojo)
   -Spendable: 2958.5843702 xcc (295858437020 mojo)

```

The only text I want is part of the total balance line:

```auto
2958.5843702 xcc

```

The number will change each time I run the command.  
Is there an easy way to achieve this?

If any easier, I could just have the whole number:

```auto
2958 xcc

```

TIA

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [16 December 2021 17:48 UTC](https://discourse.nodered.org/t/filter-certain-text-from-string/55306/2 "2021-12-16T17:48:31Z")

</div>

Hi ..

you could use the javascript `split()` method on the string. it is a string right ?

first split on `"-Total Balance: "` and then on the next space and bracket `" ("`

`msg.payload = msg.payload.split("-Total Balance: ")[1].split(" (")[0]`

**Test Flow:**

```auto
[{"id":"4167eff6f372de2a","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":180,"y":560,"wires":[["1afa79ba49f417ca"]]},{"id":"20a4b7f8e898d211","type":"function","z":"54efb553244c241f","name":"","func":"\nmsg.payload = msg.payload.split(\"-Total Balance: \")[1].split(\" (\")[0]\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":510,"y":560,"wires":[["1f8cf867e41c3b5f"]]},{"id":"1f8cf867e41c3b5f","type":"debug","z":"54efb553244c241f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":560,"wires":[]},{"id":"1afa79ba49f417ca","type":"template","z":"54efb553244c241f","name":"text","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Wallet height: 760264\nSync status: Synced\nBalances, fingerprint: 3007886911\nWallet ID 1 type STANDARD_WALLET\n -Total Balance: 2958.5843702 xcc (295858437020 mojo)\n -Pending Total Balance: 2958.5843702 xcc (295858437020 mojo)\n -Spendable: 2958.5843702 xcc (295858437020 mojo)","output":"str","x":330,"y":560,"wires":[["20a4b7f8e898d211"]]}]

```

---

<div class="post-metadata">

**Author:** ![bigmac5753](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bigmac5753/32/15733_2.png) [@bigmac5753](https://discourse.nodered.org/u/bigmac5753)\
**Post date:** [16 December 2021 18:23 UTC](https://discourse.nodered.org/t/filter-certain-text-from-string/55306/3 "2021-12-16T18:23:56Z")

</div>

Exactly what I wanted, many thanks.

---

<div class="post-metadata">

**Author:** ![dczysz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dczysz/32/46158_2.png) [@dczysz](https://discourse.nodered.org/u/dczysz)\
**Post date:** [16 December 2021 18:28 UTC](https://discourse.nodered.org/t/filter-certain-text-from-string/55306/4 "2021-12-16T18:28:27Z")

</div>

You could also use a simpler (but harder to read) regular expression:

```auto
// exec returns null if not found, or array of what matched the pattern if any are found: ["2958.5843702"]
const matches = /(?<=-Total Balance: )[\d\.]+/.exec(msg.payload);

// If not null, grab the first matching string and parse it as a number:
if (matches) msg.payload = parseFloat(matches[0])

```

---

<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:** [30 December 2021 18:28 UTC](https://discourse.nodered.org/t/filter-certain-text-from-string/55306/5 "2021-12-30T18:28:47Z")

</div>

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