# Regular Expression in Change Node Not Working

**URL:** <https://discourse.nodered.org/t/regular-expression-in-change-node-not-working/31240>\
**Category:** General\
**Created:** [6 August 2020 17:43 UTC](https://discourse.nodered.org/t/regular-expression-in-change-node-not-working/31240 "2020-08-06T17:43:51Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![gctseng](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gctseng/32/26946_2.png) [@gctseng](https://discourse.nodered.org/u/gctseng)\
**Post date:** [6 August 2020 17:43 UTC](https://discourse.nodered.org/t/regular-expression-in-change-node-not-working/31240/1 "2020-08-06T17:43:52Z")

</div>

I have created a flow to send an e-mail when a battery level is lower than a certain percentage. I used a Change node:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/b/9b7bedc7a87f7292721fcfa2e6c9ce227cd94af4.png)

The Change node gets a msg.payload from a State-Change node for a battery level, which works (e.g., msg.payload = 81 when the battery level is at 81%). I basically want to search/replace the number in msg.payload using a regular expression from:

"81"

to

"Front door lock battery level is below 50% ($1%). Please replace or recharge."  
where $1 should be the "81" or whatever level is being received.

However, the message isn't being replaced (as shown from the outputs of the Debug nodes):

 ![node Battery Level](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/e/1e2ef01a0b1fcd6338fc337b262b9590bc991337.png)

I have tried:

- ^(.\*)$
- [0-9]\*
- [0-9][0-9]

BTW, if I use "Set" instead of "Change", it works to change it completely.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [6 August 2020 17:52 UTC](https://discourse.nodered.org/t/regular-expression-in-change-node-not-working/31240/2 "2020-08-06T17:52:07Z")

</div>

not being a regex expert (I'm not even a novice, it makes my brain hurt)  
Why not do this:  
the full text of the string is:  
`Front door lock battery level is below 50% (xx%). Please replace or recharge.`

 ![Screen Shot 2020-08-06 at 1.51.00 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/0/904c7ff443fae30fa0853510b21d0aac8641c9fd.png)  
simple and easy for others to understand in 6 months.

---

<div class="post-metadata">

**Author:** ![gctseng](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gctseng/32/26946_2.png) [@gctseng](https://discourse.nodered.org/u/gctseng)\
**Post date:** [6 August 2020 18:55 UTC](https://discourse.nodered.org/t/regular-expression-in-change-node-not-working/31240/3 "2020-08-06T18:55:58Z")

</div>

Yes, that works, so I've learned a different approach. However, I am trying to use this as a stepping stone for harder expressions. I'm new to regEx, but it's like learning a new language--eventually it won't look like Greek (or regEx), haha.

---

<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 August 2020 21:01 UTC](https://discourse.nodered.org/t/regular-expression-in-change-node-not-working/31240/4 "2020-08-06T21:01:20Z")

</div>

I think the issue you came across is that payload 81 in a number not a string.

Try injecting "81" as a string as a test.

---

<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:** [6 August 2020 21:48 UTC](https://discourse.nodered.org/t/regular-expression-in-change-node-not-working/31240/5 "2020-08-06T21:48:19Z")

</div>

Why not just use a template node instead ?

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [7 August 2020 01:59 UTC](https://discourse.nodered.org/t/regular-expression-in-change-node-not-working/31240/6 "2020-08-07T01:59:41Z")

</div>

This sounds like a bug to me? Haven't used capture groups yet either so can't be sure of the syntax in this case but it seems to be partly working as `81` is there.

---

<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:** [7 August 2020 05:35 UTC](https://discourse.nodered.org/t/regular-expression-in-change-node-not-working/31240/7 "2020-08-07T05:35:47Z")

</div>

@gctseng note that you have set the "replace with" (=output) as environment variable, not as string.

But looking further;

- if input is a number, regex does not work
- if input is a string, regex works

 ![Screenshot 2020-08-07 at 07.38.27](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/9/79c0eb706cf88c93cea32fb0d3708b6be9bddda2.jpeg)

![Screenshot 2020-08-07 at 07.32.22](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/f/cf030f55da43f9af3cc8b8fab31f3f6da1e1ec33.jpeg)

Perhaps @knolleary knows how this behaves in the background, looks like regex is not treating the input as equal - or the payload type does not change, only its content.

---

<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:** [7 August 2020 21:16 UTC](https://discourse.nodered.org/t/regular-expression-in-change-node-not-working/31240/8 "2020-08-07T21:16:27Z")

</div>

Like @ristomatti said, this looks like a bug. I cannot get it to work with numeric inputs.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [7 August 2020 21:19 UTC](https://discourse.nodered.org/t/regular-expression-in-change-node-not-working/31240/9 "2020-08-07T21:19:38Z")

</div>

Not a bug as such - regular expressions only work on Strings. Whether the node should stringify non-string input is something to discuss as an enhancement to the node.

For the original example, the Template node would be a better fit, or a JSONata expression.

---

<div class="post-metadata">

**Author:** ![gctseng](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gctseng/32/26946_2.png) [@gctseng](https://discourse.nodered.org/u/gctseng)\
**Post date:** [8 August 2020 04:00 UTC](https://discourse.nodered.org/t/regular-expression-in-change-node-not-working/31240/10 "2020-08-08T04:00:43Z")

</div>

@bakman2 Tried it, but it still doesn't seem to work.

```auto
[{"id":"a119360.8311ec8","type":"server-state-changed","z":"b4a77169.bab4f8","name":"Front Door Lock Battery %","server":"61613ba9.2caa64","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.front_door_lock_be469_battery_level","entityidfiltertype":"exact","outputinitially":true,"state_type":"num","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":130,"y":720,"wires":[["3e149e54.6821ea","413a0873.0dea28"]],"icon":"font-awesome/fa-battery-2"},{"id":"3e149e54.6821ea","type":"debug","z":"b4a77169.bab4f8","name":"Battery Level","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":350,"y":780,"wires":[]},{"id":"e48f2f2b.08562","type":"debug","z":"b4a77169.bab4f8","name":"Change Msg to Create E-mail","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":650,"y":720,"wires":[]},{"id":"413a0873.0dea28","type":"change","z":"b4a77169.bab4f8","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"^(.*)$","fromt":"re","to":"Front door lock battery level is below 50% $1 %). Please replace or recharge.","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":720,"wires":[["e48f2f2b.08562"]]},{"id":"61613ba9.2caa64","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"cacheJson":false}]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/d/4d36c63e57989d707424abb82957e16099dd1a85.png)

---

<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:** [8 August 2020 04:25 UTC](https://discourse.nodered.org/t/regular-expression-in-change-node-not-working/31240/11 "2020-08-08T04:25:36Z")

</div>

Your input is still numeric, which does not work. Use a template node instead.

---

<div class="post-metadata">

**Author:** ![gctseng](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gctseng/32/26946_2.png) [@gctseng](https://discourse.nodered.org/u/gctseng)\
**Post date:** [8 August 2020 09:12 UTC](https://discourse.nodered.org/t/regular-expression-in-change-node-not-working/31240/12 "2020-08-08T09:12:50Z")

</div>

Thanks. I figured it was something like that.

---

<div class="post-metadata">

**Author:** ![gctseng](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gctseng/32/26946_2.png) [@gctseng](https://discourse.nodered.org/u/gctseng)\
**Post date:** [9 August 2020 12:59 UTC](https://discourse.nodered.org/t/regular-expression-in-change-node-not-working/31240/13 "2020-08-09T12:59:00Z")

</div>

BTW, when I set the state type to be a string on the first node, everything works as expected. Regardless, I guess I've got some learning to do with template nodes and JSON expressions

```auto
[{"id":"b4d57cfc.84d048","type":"server-state-changed","z":"50672f44.f293b8","name":"Front Door Lock Battery %","server":"61613ba9.2caa64","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.front_door_lock_be469_battery_level","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"50","halt_if_type":"num","halt_if_compare":"lt","outputs":2,"output_only_on_state_change":false,"x":130,"y":720,"wires":[["5f06c63d.7cd2c8","519ff7fc.30dbe"],["5f06c63d.7cd2c8"]],"icon":"font-awesome/fa-battery-2"},{"id":"5f06c63d.7cd2c8","type":"debug","z":"50672f44.f293b8","name":"Battery Level","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":350,"y":760,"wires":[]},{"id":"519ff7fc.30dbe","type":"change","z":"50672f44.f293b8","name":"Create e-mail message.","rules":[{"t":"change","p":"payload","pt":"msg","from":"^(.*)$","fromt":"re","to":"Battery level is below 50% ($1%). Replace/recharge.","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":720,"wires":[["464a6de3.ddc964"]]},{"id":"464a6de3.ddc964","type":"debug","z":"50672f44.f293b8","name":"Change Msg to Create E-mail","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":670,"y":760,"wires":[]},{"id":"61613ba9.2caa64","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false,"cacheJson":false}]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/0/00942e280c486b9c0cc10da2497f15939567d7d4.png)

---

<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:** [23 August 2020 12:59 UTC](https://discourse.nodered.org/t/regular-expression-in-change-node-not-working/31240/14 "2020-08-23T12:59:04Z")

</div>

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