# REGEX, Jsonata (Change node)

**URL:** <https://discourse.nodered.org/t/regex-jsonata-change-node/68487>\
**Category:** General\
**Created:** [2 October 2022 13:20 UTC](https://discourse.nodered.org/t/regex-jsonata-change-node/68487 "2022-10-02T13:20:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![JayNaire](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaynaire/32/17343_2.png) [@JayNaire](https://discourse.nodered.org/u/JayNaire)\
**Post date:** [2 October 2022 13:20 UTC](https://discourse.nodered.org/t/regex-jsonata-change-node/68487/1 "2022-10-02T13:20:27Z")

</div>

I use a change node to serve up responses to queries on a large array of unindexed notes:

```auto
$notes:=$globalContext("notes");
$searchFor:= req.query.searchFor;
$out:= $notes.**[label ~> $contains( $searchFor )] ;

```

It works well and is reasonably quick.  
Unfortunately $contains() is case sensitive and not useful for "words only" searches so I would prefer to use a regex along the lines of:

```auto
$out:= $notes.**[label ~> /($searchFor)/i] ;

```

but that, of course, just searches for "($searchFor)".  
My question is: **Can you construct, in a change node, a regex including a variable rather than a string literal?**  
(My guess is no; I've not found an example of such.)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [2 October 2022 14:23 UTC](https://discourse.nodered.org/t/regex-jsonata-change-node/68487/2 "2022-10-02T14:23:19Z")

</div>

The jsonata docs describe that $contains should be evaluated against a pattern.

Ie $contains(str, pattern)

---

<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:** [2 October 2022 14:41 UTC](https://discourse.nodered.org/t/regex-jsonata-change-node/68487/3 "2022-10-02T14:41:37Z")

</div>

> [@JayNaire](#):
>
> but that, of course, just searches for "($searchFor)".  
> My question is: **Can you construct, in a change node, a regex including a variable rather than a string literal?**  
> (My guess is no; I've not found an example of such.)

You would have to use $eval to construct the regex.  
`$eval("/(" & $searchFor & ")/i")`

You could also use $lowercase() in your original example

---

<div class="post-metadata">

**Author:** ![JayNaire](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaynaire/32/17343_2.png) [@JayNaire](https://discourse.nodered.org/u/JayNaire)\
**Post date:** [3 October 2022 21:59 UTC](https://discourse.nodered.org/t/regex-jsonata-change-node/68487/4 "2022-10-03T21:59:25Z")

</div>

> [@E1cid](#):
>
> You would have to use $eval to construct the regex.  
> `$eval("/(" & $searchFor & ")/i")`

@E1cid Brilliant. Thanks for that! A case insensitive, words only search works fine:

```auto
    $sfCIWO:= "/^[^A-Za-z0-9_]*" & $searchFor & "[^A-Za-z0-9_]*$/i" ;
    $out:= [$notes.**[label ~> ($eval($sfCIWO))] ];

```

I had to use longhand whitespace; use of valid regex metas (\w say) etc. elicits "Unsupported escape sequence: "w" . Obnoxious!

---

<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:** [3 October 2022 22:30 UTC](https://discourse.nodered.org/t/regex-jsonata-change-node/68487/5 "2022-10-03T22:30:55Z")

</div>

> [@JayNaire](#):
>
> I had to use longhand whitespace; use of valid regex metas (\w say) etc. elicits "Unsupported escape sequence: "w" . Obnoxious!

You would need to escape the `\` so `\w` would be `\\w`

---

<div class="post-metadata">

**Author:** ![JayNaire](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaynaire/32/17343_2.png) [@JayNaire](https://discourse.nodered.org/u/JayNaire)\
**Post date:** [3 October 2022 22:50 UTC](https://discourse.nodered.org/t/regex-jsonata-change-node/68487/6 "2022-10-03T22:50:44Z")

</div>

> [@E1cid](#):
>
> You would need to escape the `\` so `\w` would be `\\w`

@E1cid Mmm, yes, thank you. I did try that earlier today and the obnoxious error does disappear but it _seems_ that `\\w` is passed to regex as `\\w` not `\w` - at least I _think_ that's what happens as it didn't match correctly. I shall play again tomorrow.

---

<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:** [3 October 2022 22:59 UTC](https://discourse.nodered.org/t/regex-jsonata-change-node/68487/7 "2022-10-03T22:59:22Z")

</div>

You are incorrect `\\w \\W \\s`etc are passed as `\w \W \s`. There must have been something else wrong with your regex.

---

<div class="post-metadata">

**Author:** ![JayNaire](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaynaire/32/17343_2.png) [@JayNaire](https://discourse.nodered.org/u/JayNaire)\
**Post date:** [5 October 2022 10:35 UTC](https://discourse.nodered.org/t/regex-jsonata-change-node/68487/8 "2022-10-05T10:35:19Z")

</div>

@E1cid Good grief! The eval was correct. The regex was correct. The misleading errors were elsewhere entirely. Running fine now - many thanks for you help.

---

<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:** [19 October 2022 10:36 UTC](https://discourse.nodered.org/t/regex-jsonata-change-node/68487/9 "2022-10-19T10:36:02Z")

</div>

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