# Extract a part of a string

**URL:** <https://discourse.nodered.org/t/extract-a-part-of-a-string/89938>\
**Category:** General\
**Created:** [1 August 2024 15:09 UTC](https://discourse.nodered.org/t/extract-a-part-of-a-string/89938 "2024-08-01T15:09:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pgenoud](https://avatars.discourse-cdn.com/v4/letter/p/a587f6/32.png) [@pgenoud](https://discourse.nodered.org/u/pgenoud)\
**Post date:** [1 August 2024 15:09 UTC](https://discourse.nodered.org/t/extract-a-part-of-a-string/89938/1 "2024-08-01T15:09:08Z")

</div>

Hi all,

I have a use case that might be very easy for some of you.

I have some codes send in Node Red as String that looks like this :  
A0000013683036

I would like to extract only the part 13683036.

If my code sent is A0000213670036, I would like to extract only 213670036.

Do you know how to do it without using a function and javascript ?

Thanks a lot,  
Paul

---

<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:** [1 August 2024 15:30 UTC](https://discourse.nodered.org/t/extract-a-part-of-a-string/89938/2 "2024-08-01T15:30:54Z")

</div>

A few variables not specified.  
Do you want a string returned?  
Does it always start with `A`?  
Try

```auto
msg.payload = parseInt(msg.payload.slice(1)).toString();
return msg;

```

If you require a number returned remove the `.toString()`

Could possibly be done without JS using change node and a regex or string node or maybe a convoluted flow, but I think JS is possible the cleanest here.

Regex using change node

```auto
[{"id":"9fc7d95eb98040b7","type":"inject","z":"613df62afc8a16bf","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"A0000013683036","payloadType":"str","x":130,"y":100,"wires":[["2d1c1afc9cf699dd"]]},{"id":"2d1c1afc9cf699dd","type":"change","z":"613df62afc8a16bf","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"^[A-Z]0*","fromt":"re","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":100,"wires":[["bec5d3f7908b3aab"]]},{"id":"bec5d3f7908b3aab","type":"debug","z":"613df62afc8a16bf","name":"debug 2575","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":430,"y":140,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [1 August 2024 19:36 UTC](https://discourse.nodered.org/t/extract-a-part-of-a-string/89938/3 "2024-08-01T19:36:15Z")

</div>

Without using Javascript, but using Jsonata, which is much, much worse:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/8/38eb550c6c7fb822c7deb389bb9a84bab93632c3.png)

```auto
[{"id":"e4533461c002f3f9","type":"inject","z":"b263e6b23d8eb759","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"A00000426","payloadType":"str","x":110,"y":160,"wires":[["5f83da38286a1f62"]]},{"id":"30a822f24f791137","type":"debug","z":"b263e6b23d8eb759","name":"debug 143","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":160,"wires":[]},{"id":"5f83da38286a1f62","type":"change","z":"b263e6b23d8eb759","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"[A-Z]*[a-z]*","fromt":"re","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":160,"wires":[["7a9e7bcafc4c5611"]]},{"id":"7a9e7bcafc4c5611","type":"change","z":"b263e6b23d8eb759","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$number(payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":160,"wires":[["30a822f24f791137"]]}]

```

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [1 August 2024 23:25 UTC](https://discourse.nodered.org/t/extract-a-part-of-a-string/89938/4 "2024-08-01T23:25:47Z")

</div>

Well for those who like regex, this JSONata expression in a `change` node is pretty readable, imo... ;\*)

`payload.$match(/[A-Z]0*(\d+)/).groups[0].$number()`

---

<div class="post-metadata">

**Author:** ![pgenoud](https://avatars.discourse-cdn.com/v4/letter/p/a587f6/32.png) [@pgenoud](https://discourse.nodered.org/u/pgenoud)\
**Post date:** [2 August 2024 14:22 UTC](https://discourse.nodered.org/t/extract-a-part-of-a-string/89938/5 "2024-08-02T14:22:06Z")

</div>

Hi all,

Thanks a lot for your inputs ! I tested them all they are perfect !

---

<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:** [2 August 2024 14:24 UTC](https://discourse.nodered.org/t/extract-a-part-of-a-string/89938/6 "2024-08-02T14:24:45Z")

</div>

If you want to use JSONata the this may be cleaner

```auto
$number($substring($$.payload, 1))

```

---

<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:** [16 August 2024 14:25 UTC](https://discourse.nodered.org/t/extract-a-part-of-a-string/89938/7 "2024-08-16T14:25:06Z")

</div>

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