# Regex match in function node?

**URL:** https://discourse.nodered.org/t/regex-match-in-function-node/53605
**Category:** General
**Created:** [12 November 2021 00:13 UTC](https://discourse.nodered.org/t/regex-match-in-function-node/53605 "2021-11-12T00:13:50Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![hazymat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hazymat/32/108159_2.png) [@hazymat](https://discourse.nodered.org/u/hazymat)
#### Post date: [12 November 2021 00:13 UTC](https://discourse.nodered.org/t/regex-match-in-function-node/53605/1 "2021-11-12T00:13:50Z")

</div>

The switch node allows me to match the following regex in a string:  
`^\#\?[1-3][0-6]$`

I'd like to do this in a function node. I tried:

```auto
if (msg.payload.search("^\#\?[1-3][0-6]$") > -1) {
    // blah
    return msg;
}

```

But it doesn't work, i.e. the match is clearly not found as msg is not returned.

What's wrong here?

_(Why do I want to do this in a function node? I have a few different matches I want to perform, with different code blocks for each one, and I want to see it clearly on one page which match applies to which code block without having to switch back and forth between the switch node and the function node)_

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [12 November 2021 00:46 UTC](https://discourse.nodered.org/t/regex-match-in-function-node/53605/2 "2021-11-12T00:46:45Z")

</div>

hello .. you need slashes wrapping the regex

```auto
[{"id":"03084b6a8123d65d","type":"function","z":"5847b7aa62131d37","name":"","func":"\nmsg.payload = msg.payload.match(/^\\#\\?[1-3][0-6]$/)\n\nif (msg.payload) {\n node.warn(msg.payload[0]); // first match\n return msg;\n}\n\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":560,"y":1560,"wires":[["10663b0e50f5fd9e"]]},{"id":"eaafbd92ddb9c638","type":"inject","z":"5847b7aa62131d37","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"#?25","payloadType":"str","x":390,"y":1480,"wires":[["03084b6a8123d65d"]]},{"id":"10663b0e50f5fd9e","type":"debug","z":"5847b7aa62131d37","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":1560,"wires":[]},{"id":"d8b0fe4ae2d41355","type":"inject","z":"5847b7aa62131d37","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"#25","payloadType":"str","x":390,"y":1540,"wires":[["03084b6a8123d65d"]]},{"id":"100f24e3319532a9","type":"inject","z":"5847b7aa62131d37","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"#?55","payloadType":"str","x":390,"y":1600,"wires":[["03084b6a8123d65d"]]},{"id":"257a5fb774798e73","type":"inject","z":"5847b7aa62131d37","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"#?36","payloadType":"str","x":390,"y":1660,"wires":[["03084b6a8123d65d"]]}]

```

---

<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: [12 November 2021 00:52 UTC](https://discourse.nodered.org/t/regex-match-in-function-node/53605/3 "2021-11-12T00:52:47Z")

</div>

> [@hazymat](#):
>
> `"^\#\?[1-3][0-6]$"`

Best use `/pattern/` in javascript then you can use modifiers etc

> **[W3Schools.com](https://www.w3schools.com/jsref/jsref_obj_regexp.asp)**
>
> W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

Your regex starts with ^, which means start of string. $ at end means end of string.  
So unless your string starts with # and ends with 0-6, it will always return -1.  
Try removing the $ and ^.

---

<div class="post-metadata">

### Author: ![hazymat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hazymat/32/108159_2.png) [@hazymat](https://discourse.nodered.org/u/hazymat)
#### Post date: [12 November 2021 00:53 UTC](https://discourse.nodered.org/t/regex-match-in-function-node/53605/4 "2021-11-12T00:53:45Z")

</div>

Thanks - that solves it!

---

<div class="post-metadata">

### Author: ![hazymat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hazymat/32/108159_2.png) [@hazymat](https://discourse.nodered.org/u/hazymat)
#### Post date: [12 November 2021 00:54 UTC](https://discourse.nodered.org/t/regex-match-in-function-node/53605/5 "2021-11-12T00:54:31Z")

</div>

Yep, the regex was correct (message always starts with # and ends with 0-6 in this case...) - I just didn't use the forward slashes correctly! Thanks

---

<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: [12 November 2021 00:59 UTC](https://discourse.nodered.org/t/regex-match-in-function-node/53605/6 "2021-11-12T00:59:07Z")

</div>

The use of search() threw me off as i thought you were looking for an index of the first match. Would it not be better to use test() as it returns true or false?

---

<div class="post-metadata">

### Author: ![hazymat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hazymat/32/108159_2.png) [@hazymat](https://discourse.nodered.org/u/hazymat)
#### Post date: [12 November 2021 01:01 UTC](https://discourse.nodered.org/t/regex-match-in-function-node/53605/7 "2021-11-12T01:01:16Z")

</div>

I didn't know about that, looks like that may technically be better, although for this purpose it does pretty much the same thing I think?

---

<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: [12 November 2021 01:03 UTC](https://discourse.nodered.org/t/regex-match-in-function-node/53605/8 "2021-11-12T01:03:14Z")

</div>

```auto
if (msg.payload.test(/^\#\?[1-3][0-6]$/) ) {
    // blah
    return msg;
}

```

But cleaner in the code.

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [12 November 2021 01:20 UTC](https://discourse.nodered.org/t/regex-match-in-function-node/53605/9 "2021-11-12T01:20:04Z")

</div>

i think for test() the regex has to first

```auto
if ( /^\#\?[1-3][0-6]$/.test(msg.payload) ) {
   
 return msg;
}

```

---

<div class="post-metadata">

### Author: ![hazymat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hazymat/32/108159_2.png) [@hazymat](https://discourse.nodered.org/u/hazymat)
#### Post date: [12 November 2021 01:25 UTC](https://discourse.nodered.org/t/regex-match-in-function-node/53605/10 "2021-11-12T01:25:02Z")

</div>

I have another question now, similar topic!

I have incoming message like this:

`#>1101102582759203918490`

It consists of:  
`#` then `>` then `[1-3]` then `[1-6]` then `any 20 digits` - in that exact format.

I have regex which works in an online regex tester:  
`^\#\>[1-3][1-6]\d{20}$`

But when I do this in Node-RED function it doesn't work:

```auto
if (msg.payload.search(/^\#\>[1-3][1-6]\d{20}$/) != -1) {
    // blah
}

```

This is strange, because my first two regex matches worked in the same way. This works ok for the message `#?15`:

```auto
if (msg.payload.search(/^\#\?[1-3][0-6]$/) != -1) {
    // blah
}

```

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [12 November 2021 01:35 UTC](https://discourse.nodered.org/t/regex-match-in-function-node/53605/11 "2021-11-12T01:35:45Z")

</div>

> [@hazymat](#):
>
> `#>1101102582759203918490`

```auto
[{"id":"03084b6a8123d65d","type":"function","z":"5847b7aa62131d37","name":"","func":"\nif (/^\\#\\>[1-3][1-6]\\d{20}$/.test(msg.payload)) {\n \n return msg;\n}\n\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":580,"y":1620,"wires":[["10663b0e50f5fd9e"]]},{"id":"10663b0e50f5fd9e","type":"debug","z":"5847b7aa62131d37","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":750,"y":1620,"wires":[]},{"id":"e577b0b8725cf17c","type":"inject","z":"5847b7aa62131d37","name":"#>1101102582759203918490","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"#>1101102582759203918490","payloadType":"str","x":340,"y":1600,"wires":[["03084b6a8123d65d"]]},{"id":"5bd01e16d7f98336","type":"inject","z":"5847b7aa62131d37","name":"#>11011025827","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"#>11011025827","payloadType":"str","x":360,"y":1660,"wires":[["03084b6a8123d65d"]]}]

```

---

<div class="post-metadata">

### Author: ![hazymat](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hazymat/32/108159_2.png) [@hazymat](https://discourse.nodered.org/u/hazymat)
#### Post date: [12 November 2021 01:51 UTC](https://discourse.nodered.org/t/regex-match-in-function-node/53605/12 "2021-11-12T01:51:22Z")

</div>

> [@UnborN](#):
>
> ```auto
> [{"id":"03084b6a8123d65d","type":"function","z":"5847b7aa62131d37","name":"","func":"\nif (/^\\#\\>[1-3][1-6]\\d{20}$/.test(msg.payload)) {\n \n return msg;\n}\n\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":580,"y":1620,"wires":[["10663b0e50f5fd9e"]]},{"id":"10663b0e50f5fd9e","type":"debug","z":"5847b7aa62131d37","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":750,"y":1620,"wires":[]},{"id":"e577b0b8725cf17c","type":"inject","z":"5847b7aa62131d37","name":"#>1101102582759203918490","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"#>1101102582759203918490","payloadType":"str","x":340,"y":1600,"wires":[["03084b6a8123d65d"]]},{"id":"5bd01e16d7f98336","type":"inject","z":"5847b7aa62131d37","name":"#>11011025827","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"#>11011025827","payloadType":"str","x":360,"y":1660,"wires":[["03084b6a8123d65d"]]}]
> 
> ```

Ahh - found the problem. My device was returning an additional space at the end 🙂 Thanks

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [12 November 2021 01:52 UTC](https://discourse.nodered.org/t/regex-match-in-function-node/53605/13 "2021-11-12T01:52:51Z")

</div>

i was wondering because i tested with search() syntax also and it was working for me 😉

---

<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: [26 November 2021 01:53 UTC](https://discourse.nodered.org/t/regex-match-in-function-node/53605/14 "2021-11-26T01:53:17Z")

</div>

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