# Usage of regex in JSONata

**URL:** <https://discourse.nodered.org/t/usage-of-regex-in-jsonata/55866>\
**Category:** General\
**Created:** [29 December 2021 22:49 UTC](https://discourse.nodered.org/t/usage-of-regex-in-jsonata/55866 "2021-12-29T22:49:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bs-eng](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bs-eng/32/51470_2.png) [@bs-eng](https://discourse.nodered.org/u/bs-eng)\
**Post date:** [29 December 2021 22:49 UTC](https://discourse.nodered.org/t/usage-of-regex-in-jsonata/55866/1 "2021-12-29T22:49:03Z")

</div>

Hi there!  
I guess I am mediocre at programming - mostly on ANSI C and a bit object oriented.  
Node-red is still fairly new to me.

For a Rest API call with multiple requests I am trying to modify the url to make the second request. In order to do so I need to modify values in the url-string and replace them with calculated values.

From an example I copied and modified the nodes and it works fine - appart from the url modification.  
The JSONata code is:

```auto
$replace(
   url,
   /(limit=)(.+)(&offset=)(.+)/,
   "$1" & "$2" & "$3" & $string( $number($4) + 50)
)

```

For the url-string:  
`"url": "https://mywebsite/?limit=50&offset=100"`

I would expect a result like this:  
`https://mywebsite/?limit=50&offset=150`

What I am getting is missing the correct offset value.  
`https://mywebsite/?limit=50&offset=`

I checked $4 it does indeed contain the former offset value of 100. To me it seems I am getting the syntax somehow wrong to address the $4 properly. I tried a million different formats that I found online, to no avail.

Can someone point me in the right direction please?  
Thanks a lot!!  
Cheers

---

<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:** [30 December 2021 00:27 UTC](https://discourse.nodered.org/t/usage-of-regex-in-jsonata/55866/2 "2021-12-30T00:27:24Z")

</div>

`$4` is not in the string replacement so is not seen by the regex replacement.  
You would need to use `$match()` and bind a variable. Then you can use `$number()` on index 3  
e.g.

```auto
($m := $match(url, /(limit=)(.+)(&offset=)(.+)/);
$r := $m.groups;
$replace(
   url,
   $m.match,
     $r[0] & $r[1] & $r[2] & ($number($r[3]) + 50)
)
)

```

---

<div class="post-metadata">

**Author:** ![bs-eng](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bs-eng/32/51470_2.png) [@bs-eng](https://discourse.nodered.org/u/bs-eng)\
**Post date:** [7 January 2022 18:53 UTC](https://discourse.nodered.org/t/usage-of-regex-in-jsonata/55866/3 "2022-01-07T18:53:37Z")

</div>

Thx!  
this worked for me as well as an alternative solution where I used variable in the respective string and dynamicall replace them with the correct value before use. The correct value I calculate from a msg element that is passed through the whole flow.

---

<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:** [8 March 2022 18:54 UTC](https://discourse.nodered.org/t/usage-of-regex-in-jsonata/55866/4 "2022-03-08T18:54:12Z")

</div>

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