# Help with regex replace

**URL:** <https://discourse.nodered.org/t/help-with-regex-replace/34933>\
**Category:** General\
**Created:** [28 October 2020 07:40 UTC](https://discourse.nodered.org/t/help-with-regex-replace/34933 "2020-10-28T07:40:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hunter](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hunter/32/60045_2.png) [@Hunter](https://discourse.nodered.org/u/Hunter)\
**Post date:** [28 October 2020 07:40 UTC](https://discourse.nodered.org/t/help-with-regex-replace/34933/1 "2020-10-28T07:40:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [28 October 2020 08:00 UTC](https://discourse.nodered.org/t/help-with-regex-replace/34933/2 "2020-10-28T08:00:17Z")

</div>

Try this example

```auto
[{"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"]]}]

```

---

<div class="post-metadata">

**Author:** ![Hunter](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hunter/32/60045_2.png) [@Hunter](https://discourse.nodered.org/u/Hunter)\
**Post date:** [28 October 2020 11:51 UTC](https://discourse.nodered.org/t/help-with-regex-replace/34933/3 "2020-10-28T11:51:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [28 October 2020 11:57 UTC](https://discourse.nodered.org/t/help-with-regex-replace/34933/4 "2020-10-28T11:57:55Z")

</div>

Hi @Hunter

yes, you could do:

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

```

---

<div class="post-metadata">

**Author:** ![Hunter](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hunter/32/60045_2.png) [@Hunter](https://discourse.nodered.org/u/Hunter)\
**Post date:** [28 October 2020 12:42 UTC](https://discourse.nodered.org/t/help-with-regex-replace/34933/5 "2020-10-28T12:42:22Z")

</div>

Perfect, thanks! 🙂

---

<div class="post-metadata">

**Author:** ![Hunter](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hunter/32/60045_2.png) [@Hunter](https://discourse.nodered.org/u/Hunter)\
**Post date:** [28 October 2020 19:10 UTC](https://discourse.nodered.org/t/help-with-regex-replace/34933/6 "2020-10-28T19:10:41Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![Hunter](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hunter/32/60045_2.png) [@Hunter](https://discourse.nodered.org/u/Hunter)\
**Post date:** [28 October 2020 19:13 UTC](https://discourse.nodered.org/t/help-with-regex-replace/34933/7 "2020-10-28T19:13:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [28 October 2020 19:56 UTC](https://discourse.nodered.org/t/help-with-regex-replace/34933/8 "2020-10-28T19:56:06Z")

</div>

> [@Hunter](#):
>
> Between the Day and : can be zero or several spaces

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

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

```

---

<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:** [11 November 2020 19:56 UTC](https://discourse.nodered.org/t/help-with-regex-replace/34933/9 "2020-11-11T19:56:10Z")

</div>

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