More regex guidance

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=

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.

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

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

here is a Javascript and a JSONata example.

[{"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

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

JSONata

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

[edit]

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

1 Like

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

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