# Parse string to get a link

**URL:** <https://discourse.nodered.org/t/parse-string-to-get-a-link/56753>\
**Category:** General\
**Created:** [16 January 2022 16:56 UTC](https://discourse.nodered.org/t/parse-string-to-get-a-link/56753 "2022-01-16T16:56:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lafayette](https://avatars.discourse-cdn.com/v4/letter/l/47e85d/32.png) [@Lafayette](https://discourse.nodered.org/u/Lafayette)\
**Post date:** [16 January 2022 16:56 UTC](https://discourse.nodered.org/t/parse-string-to-get-a-link/56753/1 "2022-01-16T16:56:53Z")

</div>

Hi, i'm trying to get a link inside the payload of the Twitter node : "Tonga : Animation satellite qui montre l'explosion suivie d'un important panache de fumée qui s'est élevée dans l'atmosphère suite à l'éruption du volcan Hunga Tonga-Hunga-Ha'apai et qui a déclenché une alerte tsunami. **[https://twitter.com/RebeccaRambar/status/1482360722105511946/video/1](https://t.co/eWg9v5di23)**"

But i don't know how to do that for all the tweets i receive.  
Thx.

---

<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 January 2022 17:44 UTC](https://discourse.nodered.org/t/parse-string-to-get-a-link/56753/2 "2022-01-16T17:44:23Z")

</div>

Hello .. you can use the power of Regular Expressions in a Function node to filter from your string any link  
In the example below an array is produced with all matching links

**Example Flow:**

```auto
[{"id":"53816c0d1d6d081a","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":270,"y":1020,"wires":[["0eae0d0c48f8a13d"]]},{"id":"2bf55b03434e0438","type":"function","z":"54efb553244c241f","name":"","func":"msg.payload = msg.payload.match(/http[s]?:\\/\\/[^\\s]+/g)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":620,"y":1020,"wires":[["8df9347709fc5181"]]},{"id":"8df9347709fc5181","type":"debug","z":"54efb553244c241f","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":770,"y":1020,"wires":[]},{"id":"0eae0d0c48f8a13d","type":"template","z":"54efb553244c241f","name":"data","field":"payload","fieldType":"msg","format":"text","syntax":"plain","template":"Tonga : Animation satellite qui montre l'explosion suivie d'un important panache de fumée qui s'est élevée dans l'atmosphère suite à l'éruption du volcan Hunga Tonga-Hunga-Ha'apai et qui a déclenché une alerte tsunami. https://twitter.com/RebeccaRambar/status/1482360722105511946/video/1\nTonga : https://twitter.com/RebeccaRambar/status/43534534/video/2 Animation satellite qui montre l'explosion suivie d'un important panache de fumée qui s'est élevée dans l'atmosphère suite à l'éruption du volcan Hunga Tonga-Hunga-Ha'apai et qui a déclenché une alerte tsunami. ","output":"str","x":430,"y":1020,"wires":[["2bf55b03434e0438","4a70e2267055ff0e"]]},{"id":"4a70e2267055ff0e","type":"debug","z":"54efb553244c241f","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":510,"y":940,"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:** [16 January 2022 17:44 UTC](https://discourse.nodered.org/t/parse-string-to-get-a-link/56753/3 "2022-01-16T17:44:50Z")

</div>

Possibly something like this in a function node:

```auto
let regex = '(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?';
let index = msg.payload.search(regex); 
let payload = msg.payload.substring(index);
msg.payload = payload.split(' ')[0]; // Drop anything from a space character after URL
return msg;

```

You will probably want to filter out tweets which don't include a URL (index == -1)

Edit - Snap - almost!

---

<div class="post-metadata">

**Author:** ![Lafayette](https://avatars.discourse-cdn.com/v4/letter/l/47e85d/32.png) [@Lafayette](https://discourse.nodered.org/u/Lafayette)\
**Post date:** [16 January 2022 23:52 UTC](https://discourse.nodered.org/t/parse-string-to-get-a-link/56753/4 "2022-01-16T23:52:31Z")

</div>

> [@jbudd](#):
>
> ```auto
> let regex = '(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?';
> let index = msg.payload.search(regex); 
> let payload = msg.payload.substring(index);
> msg.payload = payload.split(' ')[0]; // Drop anything from a space character after URL
> return msg;
> 
> ```

Thx ! I'm going to try it.

---

<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:** [17 March 2022 23:53 UTC](https://discourse.nodered.org/t/parse-string-to-get-a-link/56753/5 "2022-03-17T23:53:07Z")

</div>

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