Find replace

1,702 views
Skip to first unread message

Chris Dobson

unread,
Oct 7, 2016, 7:50:10 AM10/7/16
to Node-RED
Hi i am trying to remove underscores and replace them with a space, but cant get it to worki have a function as follows:

var msg = {payload:msg.payload.replace(/"_"/g, " ")};

return msg;

Can anyone help?
thanks

Dave C-J

unread,
Oct 7, 2016, 7:52:53 AM10/7/16
to node...@googlegroups.com

Chris Dobson

unread,
Oct 7, 2016, 8:10:32 AM10/7/16
to Node-RED
i tried that first:

var msg = {payload:msg.payload};
var msg2 = msg.replace(/"_"/g, " ");
return msg2;

but just get a TypeError: msg.replace is not a function

Zenofmud

unread,
Oct 7, 2016, 8:16:43 AM10/7/16
to node...@googlegroups.com
That is because msg.replace() is not a function while replace() is… 

-- 
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
--- 
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/d9fff228-7d0b-4f37-812b-1db8105d0ed2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Colin Law

unread,
Oct 7, 2016, 8:18:17 AM10/7/16
to node...@googlegroups.com
You need to look at the example again. Your first go was nearly
right, the difference is that you have quotes round the underscore
which means it is looking for "_" including the quotes.

Colin

On 7 October 2016 at 13:10, 'Chris Dobson' via Node-RED

Nicholas O'Leary

unread,
Oct 7, 2016, 8:18:17 AM10/7/16
to Node-RED Mailing List
Hi Chris,

the problem in your code was too many quotes in the regular expression you're passing to msg.payload.replace()

    var msg = {payload:msg.payload.replace(/_/g, " ")};
    return msg;

But note, in general you should reuse the message passed in rather than create a new message object. So the following would be better:

    msg.payload = msg.payload.replace(/_/g, " ");
    return msg;


Nick

On 7 October 2016 at 13:10, 'Chris Dobson' via Node-RED <node...@googlegroups.com> wrote:

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.

Zenofmud

unread,
Oct 7, 2016, 8:20:03 AM10/7/16
to node...@googlegroups.com
ummm ignore me (sheepish grin)

Chris Dobson

unread,
Oct 7, 2016, 8:44:04 AM10/7/16
to Node-RED
thankyou !
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.

Chris Dobson

unread,
Oct 7, 2016, 8:46:11 AM10/7/16
to Node-RED
thank you


On Friday, October 7, 2016 at 1:18:17 PM UTC+1, Nick O'Leary wrote:
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages