# More regex guidance

**URL:** <https://discourse.nodered.org/t/more-regex-guidance/53304>\
**Category:** General\
**Created:** [6 November 2021 18:17 UTC](https://discourse.nodered.org/t/more-regex-guidance/53304 "2021-11-06T18:17:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Romprod](https://avatars.discourse-cdn.com/v4/letter/r/e47774/32.png) [@Romprod](https://discourse.nodered.org/u/Romprod)\
**Post date:** [6 November 2021 18:17 UTC](https://discourse.nodered.org/t/more-regex-guidance/53304/1 "2021-11-06T18:17:13Z")

</div>

Hey guys, I'm back again struggling to understand how to format some more text

I have the following that I'm trying to split up and comma delimit (if that's the correct phrase)

msg.payload=

```auto
1Chelsea1 Burnley,0Manchester United2 Manchester City,2Crystal Palace0 Wolves,1Brentford2 Norwich City,1Brighton & Hove Albion0 Newcastle United

```

I tried using the function node to search with regular expressions but there's only an option to replace rather than insert which is my limit (is there another change node that could insert instead of replace?)

I'd like to achieve the following result (Basically surround each number in a single comma so that I can then build an array with the output.

```auto
1,Chelsea,1 ,Burnley,0,Manchester United,2 ,Manchester City,2,Crystal Palace,0 ,Wolves,1,Brentford,2 ,Norwich City,1,Brighton & Hove Albion,0 ,Newcastle United

```

I'm struggling to understand how I would use regex to find text and then insert something before or after the text

---

<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:** [6 November 2021 18:45 UTC](https://discourse.nodered.org/t/more-regex-guidance/53304/2 "2021-11-06T18:45:55Z")

</div>

Why add the comma's, why not just create the array.

here is a Javascript and a JSONata example.

```auto
[{"id":"f1476130.4f0db","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1Chelsea1 Burnley,0Manchester United2 Manchester City,2Crystal Palace0 Wolves,1Brentford2 Norwich City,1Brighton & Hove Albion0 Newcastle United","payloadType":"str","x":120,"y":160,"wires":[["aa2369ab.e97328","acb0a726.cc26f"]]},{"id":"aa2369ab.e97328","type":"function","z":"b779de97.b1b46","name":"","func":"msg.payload = msg.payload.match(/(\\d+|[^\\d,]+)/g).map(e => e.trim())\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":260,"y":180,"wires":[["dce26853.cddf68"]]},{"id":"acb0a726.cc26f","type":"change","z":"b779de97.b1b46","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$match($$.payload, /(\\d+|[^\\d,]+)/).match.$trim()\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":140,"wires":[["dce26853.cddf68"]]},{"id":"dce26853.cddf68","type":"debug","z":"b779de97.b1b46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":450,"y":180,"wires":[]}]

```

Javascript

```auto
msg.payload.match(/(\d+|[^\d,]+)/g).map(e => e.trim())

```

JSONata

```auto
$match($$.payload, /(\d+|[^\d,]+)/).match.$trim()	

```

[edit]

> [@Romprod](#):
>
> than insert which is my limit (is there another change node that could insert instead of replace?)

You can use capture groups in the regex search `/a(b)/`then in the replace use `$1` in the replace, i.e. `$1c`. if input was 'ab' output would be bc. Each capture group `()` increments so if you had 2, the second would be `$2`

---

<div class="post-metadata">

**Author:** ![Romprod](https://avatars.discourse-cdn.com/v4/letter/r/e47774/32.png) [@Romprod](https://discourse.nodered.org/u/Romprod)\
**Post date:** [6 November 2021 19:59 UTC](https://discourse.nodered.org/t/more-regex-guidance/53304/3 "2021-11-06T19:59:38Z")

</div>

Ah thankyou so much for the explanation, that'll help me figure a lot of things out.

---

<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:** [20 November 2021 20:00 UTC](https://discourse.nodered.org/t/more-regex-guidance/53304/4 "2021-11-20T20:00:01Z")

</div>

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