# How to extract characters using Javascript in a function?

**URL:** <https://discourse.nodered.org/t/how-to-extract-characters-using-javascript-in-a-function/44435>\
**Category:** General\
**Created:** [20 April 2021 05:58 UTC](https://discourse.nodered.org/t/how-to-extract-characters-using-javascript-in-a-function/44435 "2021-04-20T05:58:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MercierFab](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mercierfab/32/40493_2.png) [@MercierFab](https://discourse.nodered.org/u/MercierFab)\
**Post date:** [20 April 2021 05:58 UTC](https://discourse.nodered.org/t/how-to-extract-characters-using-javascript-in-a-function/44435/1 "2021-04-20T05:58:37Z")

</div>

Hello, I need to extract 2 characters from a payload (string) using substr(x,y) in a function node and it doesn't work. ex : var temperature = payload.substr(2,4). The function doesn't exist. Should I install a package or use an other function ? Do you have a tip? Or at least how to extract one character from a string?  
I have Node red V1.3.2.  
Thanks for the answer.

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [20 April 2021 06:04 UTC](https://discourse.nodered.org/t/how-to-extract-characters-using-javascript-in-a-function/44435/2 "2021-04-20T06:04:33Z")

</div>

Dont know if you just miss typed but:

> [@MercierFab](#):
>
> var temperature = payload.substr(2,4)

should be:

```auto
var temperature = msg.payload.substr(2,4);

```

as only using payload will give you not a function as you need to qualify the whole object path of a msg object property not just the key itself.

Johannes

---

<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:** [20 April 2021 06:35 UTC](https://discourse.nodered.org/t/how-to-extract-characters-using-javascript-in-a-function/44435/3 "2021-04-20T06:35:15Z")

</div>

Also if you just want 2 characters it should be `msg.payload.substr(2,2)`

---

<div class="post-metadata">

**Author:** ![MercierFab](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mercierfab/32/40493_2.png) [@MercierFab](https://discourse.nodered.org/u/MercierFab)\
**Post date:** [20 April 2021 16:55 UTC](https://discourse.nodered.org/t/how-to-extract-characters-using-javascript-in-a-function/44435/4 "2021-04-20T16:55:46Z")

</div>

Thanks both of you for the answer it's clear, it works.

Fabrice

---

<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:** [19 June 2021 16:56 UTC](https://discourse.nodered.org/t/how-to-extract-characters-using-javascript-in-a-function/44435/5 "2021-06-19T16:56:42Z")

</div>

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