# Extract URL from email

**URL:** <https://discourse.nodered.org/t/extract-url-from-email/38129>\
**Category:** General\
**Created:** [28 December 2020 10:21 UTC](https://discourse.nodered.org/t/extract-url-from-email/38129 "2020-12-28T10:21:01Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigmac5753](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bigmac5753/32/15733_2.png) [@bigmac5753](https://discourse.nodered.org/u/bigmac5753)\
**Post date:** [28 December 2020 10:21 UTC](https://discourse.nodered.org/t/extract-url-from-email/38129/1 "2020-12-28T10:21:01Z")

</div>

i'm using node-red-node-email to read email.

What I want is to extract a URL from the email body, and send only that onwards, how can I do this?

---

<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:** [28 December 2020 10:40 UTC](https://discourse.nodered.org/t/extract-url-from-email/38129/2 "2020-12-28T10:40:59Z")

</div>

Unless the URL is in the exact same place each time, probably best to use a regular expression.

E.g...

"Regular expression to find URLs within a string - Stack Overflow" [https://stackoverflow.com/questions/6038061/regular-expression-to-find-urls-within-a-string](https://stackoverflow.com/questions/6038061/regular-expression-to-find-urls-within-a-string)

---

<div class="post-metadata">

**Author:** ![bigmac5753](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bigmac5753/32/15733_2.png) [@bigmac5753](https://discourse.nodered.org/u/bigmac5753)\
**Post date:** [28 December 2020 19:17 UTC](https://discourse.nodered.org/t/extract-url-from-email/38129/3 "2020-12-28T19:17:40Z")

</div>

tried regex but it doesn't work

---

<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:** [28 December 2020 19:19 UTC](https://discourse.nodered.org/t/extract-url-from-email/38129/4 "2020-12-28T19:19:44Z")

</div>

Sure it does. Post a sample string containing a URL.

---

<div class="post-metadata">

**Author:** ![bigmac5753](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bigmac5753/32/15733_2.png) [@bigmac5753](https://discourse.nodered.org/u/bigmac5753)\
**Post date:** [28 December 2020 19:26 UTC](https://discourse.nodered.org/t/extract-url-from-email/38129/5 "2020-12-28T19:26:50Z")

</div>

I tried a few examples from your link, they either still allow the whole message through or nothing at all. the URL format is always: `https://example.com/page/` it never has www and sometimes has an underscore within `/page/`.

---

<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:** [28 December 2020 19:40 UTC](https://discourse.nodered.org/t/extract-url-from-email/38129/6 "2020-12-28T19:40:16Z")

</div>

I copied one of the `regex` from that page i linked to & it worked...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/e/9e2cc8c4653892cf5ffdecf389d8e8b1ceafed62.png)

```auto
msg.payload = msg.payload.match(/(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$])/igm);
return msg;

```

---

<div class="post-metadata">

**Author:** ![bigmac5753](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bigmac5753/32/15733_2.png) [@bigmac5753](https://discourse.nodered.org/u/bigmac5753)\
**Post date:** [28 December 2020 19:46 UTC](https://discourse.nodered.org/t/extract-url-from-email/38129/7 "2020-12-28T19:46:02Z")

</div>

Thanks, that works.

I was using the switch node....  
 ![Screen Shot 2020-12-28 at 19.45.00](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/d/ede32827d2e48b53770799853ba0c064dcd6e827.png)

---

<div class="post-metadata">

**Author:** ![bigmac5753](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bigmac5753/32/15733_2.png) [@bigmac5753](https://discourse.nodered.org/u/bigmac5753)\
**Post date:** [29 December 2020 10:30 UTC](https://discourse.nodered.org/t/extract-url-from-email/38129/8 "2020-12-29T10:30:44Z")

</div>

Out of curiosity, how would I do it if it was in the same place every time?

---

<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:** [29 December 2020 10:34 UTC](https://discourse.nodered.org/t/extract-url-from-email/38129/9 "2020-12-29T10:34:57Z")

</div>

You could use JS string functions like `indexOf` and/or `split` to break up the string and find the URL. Just stick with the regex if it works

---

<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:** [12 January 2021 10:35 UTC](https://discourse.nodered.org/t/extract-url-from-email/38129/10 "2021-01-12T10:35:04Z")

</div>

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