# Linkify best way to impliment/ function 'replace' error

**URL:** <https://discourse.nodered.org/t/linkify-best-way-to-impliment-function-replace-error/45376>\
**Category:** General\
**Created:** [8 May 2021 21:51 UTC](https://discourse.nodered.org/t/linkify-best-way-to-impliment-function-replace-error/45376 "2021-05-08T21:51:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ninjamoba](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ninjamoba/32/31505_2.png) [@ninjamoba](https://discourse.nodered.org/u/ninjamoba)\
**Post date:** [8 May 2021 21:51 UTC](https://discourse.nodered.org/t/linkify-best-way-to-impliment-function-replace-error/45376/1 "2021-05-08T21:51:52Z")

</div>

Want to "linkafy" text thats being generated into an HTML from node-red  
am getting an error from a function node that seems node-red related

function : (error)

"TypeError: Cannot read property 'replace' of undefined"

```auto
function linkify(themessage) {
    var replacedText, replacePattern1, replacePattern2, replacePattern3;

    //URLs starting with http://, https://, or ftp://
    replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
    replacedText = themessage.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');

    //URLs starting with "www." (without // before it, or it'd re-link the ones done above).
    replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
    replacedText = themessage.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');

    //Change email addresses to mailto:: links.
    replacePattern3 = /(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim;
    replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');

    return replacedText;
}
msg.payload.message = linkify(msg.payload.message);
return msg;

```

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [8 May 2021 22:04 UTC](https://discourse.nodered.org/t/linkify-best-way-to-impliment-function-replace-error/45376/2 "2021-05-08T22:04:37Z")

</div>

May happen if incoming `msg.payload` has no `message` property.  
Put debug BEFORE the function node to see what kind of input is feed to the function or use debugging options inside the function  
`node.warn('incoming message:'+msg.payload.message)`

---

<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:** [7 July 2021 22:04 UTC](https://discourse.nodered.org/t/linkify-best-way-to-impliment-function-replace-error/45376/3 "2021-07-07T22:04:55Z")

</div>

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