# Newbie help – split text and store in variables

**URL:** <https://discourse.nodered.org/t/newbie-help-split-text-and-store-in-variables/27319>\
**Category:** General\
**Created:** [26 May 2020 14:58 UTC](https://discourse.nodered.org/t/newbie-help-split-text-and-store-in-variables/27319 "2020-05-26T14:58:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chloroquine](https://avatars.discourse-cdn.com/v4/letter/c/dec6dc/32.png) [@chloroquine](https://discourse.nodered.org/u/chloroquine)\
**Post date:** [26 May 2020 14:58 UTC](https://discourse.nodered.org/t/newbie-help-split-text-and-store-in-variables/27319/1 "2020-05-26T14:58:26Z")

</div>

Using node-red, I receive messages from Telegram which are formatted like:

```
{
  "payload": {
    "chatId": 12345,
    "messageId": 1,
    "type": "message",
    "content": "Hi_._MainChat"
  },
  "originalMessage": {
    "message_id": 1,
    "from": {
      "id": 1234567890,
      "is_bot": false,
      "first_name": "John",
      "username": "JohnDoe",
      "language_code": "en"
    },
    "chat": {
      "id": 12345,
      "first_name": "John",
      "username": "JohnDoe",
      "type": "private"
    },
    "date": 1590504750,
    "text": "Hi_._MainChat"
  },
  "_msgid": "3be22fdd.3c9f5"
}

```

I want to **split** the `content` at '`_._`' and store the left ("_Hi_") and the right ("_MainChat_") part in two variables for later use. Could one be so kind and assist me how to do this?

I tried a function like

```auto
// Split the message content to extract text and origin
var text = {};
text.payload = (msg.originalMessage.text.split("_._"))[0];
text.type = "text";

var origin = {};
origin.payload = (msg.originalMessage.text.split("_._"))[1];
origin.type = "origin";

msg.payload.text = text.payload;
msg.payload.origin = origin.payload;

return [text,origin];

```

but failed miserably.

---

<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 May 2020 15:27 UTC](https://discourse.nodered.org/t/newbie-help-split-text-and-store-in-variables/27319/2 "2020-05-26T15:27:42Z")

</div>

> [@chloroquine](#):
>
> but failed miserably.

a little bit of pointless code but not too far off.

```auto
// Split the message content to extract text and origin
var parts = msg.originalMessage.text.split("_._");
msg.payload.text = parts[0];
msg.payload.origin = parts[1];
return msg;//return the updated msg for the next node to access msg.payload.text & msg.payload.origin

//// or if you REALLY REALLY want two messages to be sent from the function then...
//var msg1 = {payload: parts[0]};
//var msg2 = {payload: parts[1]};
//return[msg1,msg2]

```

---

<div class="post-metadata">

**Author:** ![chloroquine](https://avatars.discourse-cdn.com/v4/letter/c/dec6dc/32.png) [@chloroquine](https://discourse.nodered.org/u/chloroquine)\
**Post date:** [26 May 2020 20:03 UTC](https://discourse.nodered.org/t/newbie-help-split-text-and-store-in-variables/27319/3 "2020-05-26T20:03:57Z")

</div>

Thanks a lot!

---

<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:** [9 June 2020 20:04 UTC](https://discourse.nodered.org/t/newbie-help-split-text-and-store-in-variables/27319/4 "2020-06-09T20:04:02Z")

</div>

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