Help with regex replace

Hello,

I'm parsing a buffer with several lines of content, like;

Monday: John
Tuesday: Michael
Wednesday: Martin
etc

I would like the first word, in this example the day, to be matched. My words allways start with a capital and there are some cases with just one single letter which should not be matched, so lets say 3 letters and more. The replacement needs to be put in HTML tags, like Monday The : has to be excluded

Try this example

[{"id":"93f59ed1.e79b9","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"My: John","payloadType":"str","x":190,"y":3200,"wires":[["71868977.626ca8"]]},{"id":"a6253f9c.4b7628","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Monday: John","payloadType":"str","x":173.61111450195312,"y":3262.777587890625,"wires":[["71868977.626ca8"]]},{"id":"dd13836e.943148","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"monday: John","payloadType":"str","x":172.77777099609375,"y":3317.777587890625,"wires":[["71868977.626ca8"]]},{"id":"71868977.626ca8","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"^([A-Z]\\w{2,})(:.*)?","fromt":"re","to":"<$1>$2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":3240,"wires":[["d563006b.aa5ac8"]]}]
1 Like

Can this also be used in a function as string.replace?

Hi @Hunter

yes, you could do:

msg.payload = msg.payload.replace(/(^|\n)([A-Z]\w{2,}\w*):/g, "$1<b>$2<b>:");
return msg;
1 Like

Perfect, thanks! :slight_smile:

Sorry, is not working quite as expected, only the first line is replaced. My string is like:

"Monday: John\nTuesday: Michael\nWednesday: Martin\n"

Sorry I think I need to clarify something... Between the Day and : can be zero or several spaces

In which case, try with the following regex in the replace function callL:

/(^|\n)([A-Z]\w{2,}\w*) *:/g
1 Like

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