# Extract a Substring from msg.payload and use it as a variable for global.set?

**URL:** <https://discourse.nodered.org/t/extract-a-substring-from-msg-payload-and-use-it-as-a-variable-for-global-set/77687>\
**Category:** General\
**Tags:** function-node\
**Created:** [18 April 2023 12:27 UTC](https://discourse.nodered.org/t/extract-a-substring-from-msg-payload-and-use-it-as-a-variable-for-global-set/77687 "2023-04-18T12:27:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [18 April 2023 12:27 UTC](https://discourse.nodered.org/t/extract-a-substring-from-msg-payload-and-use-it-as-a-variable-for-global-set/77687/1 "2023-04-18T12:27:59Z")

</div>

Hi,

is there a chance to extract a value from a string, e.g.

```auto
tele/SP111_131308/#

```

(i need the value between the slashes)

and would like to use it as a global Variable:

```auto
global.set ("SP111_131308.State", msg.payload.Switch_State);

```

Is this even possible in NR ?  
BR  
Gawan

---

<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 April 2023 12:33 UTC](https://discourse.nodered.org/t/extract-a-substring-from-msg-payload-and-use-it-as-a-variable-for-global-set/77687/2 "2023-04-18T12:33:26Z")

</div>

> [@Gawan](#):
>
> ```auto
> global.set ("SP111_131308.State", msg.payload.Switch_State);
> 
> ```
> 
> Is this even possible in NR ?

```auto
let topic = msg.topic.split("/")[1];
global.set (topic + ".State", msg.payload.Switch_State);

```

---

<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:** [2 May 2023 12:34 UTC](https://discourse.nodered.org/t/extract-a-substring-from-msg-payload-and-use-it-as-a-variable-for-global-set/77687/3 "2023-05-02T12:34:24Z")

</div>

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