# Regex.test alternate between true and false notwithstanding the value being always the same

**URL:** <https://discourse.nodered.org/t/regex-test-alternate-between-true-and-false-notwithstanding-the-value-being-always-the-same/94678>\
**Category:** General\
**Created:** [16 January 2025 15:02 UTC](https://discourse.nodered.org/t/regex-test-alternate-between-true-and-false-notwithstanding-the-value-being-always-the-same/94678 "2025-01-16T15:02:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhamel](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@jhamel](https://discourse.nodered.org/u/jhamel)\
**Post date:** [16 January 2025 15:02 UTC](https://discourse.nodered.org/t/regex-test-alternate-between-true-and-false-notwithstanding-the-value-being-always-the-same/94678/1 "2025-01-16T15:02:43Z")

</div>

In the below flow, I have an array with file name. From a **function** node, I use **regex.test** to check if the name matches a format. For some unknown reason, while checking this in **array.forEach** , the result alternate between true and false, although the value is always the same.  
In my original flow, the values are different but the result is the same. I only used the same value to emphasize the problem.

```auto
[{"id":"de9b52e1a522fb37","type":"tab","label":"Flow 3","disabled":false,"info":"","env":[]},{"id":"1067019b063d7a3d","type":"function","z":"de9b52e1a522fb37","name":"Check file name","func":"const theMatch = /\\.pp[4567]$/gi\nvar theResult = true;\nnode.warn(msg.payload)\nmsg.payload.forEach(element => {\n theResult = theMatch.test(element.originalfilename);\n node.warn(element.originalfilename);\n node.warn(theResult);\n});\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":600,"y":340,"wires":[["a812dc805065ecd9"]]},{"id":"9531e84f57e9dbb4","type":"inject","z":"de9b52e1a522fb37","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"originalfilename\":\"file1.pp7\"},{\"originalfilename\":\"file1.pp7\"},{\"originalfilename\":\"file1.pp7\"},{\"originalfilename\":\"file1.pp7\"}]","payloadType":"json","x":270,"y":340,"wires":[["1067019b063d7a3d","328b48aa4ee8fb75"]]},{"id":"328b48aa4ee8fb75","type":"debug","z":"de9b52e1a522fb37","name":"injected","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":420,"y":260,"wires":[]},{"id":"a812dc805065ecd9","type":"debug","z":"de9b52e1a522fb37","name":"result","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":790,"y":340,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [16 January 2025 15:04 UTC](https://discourse.nodered.org/t/regex-test-alternate-between-true-and-false-notwithstanding-the-value-being-always-the-same/94678/2 "2025-01-16T15:04:51Z")

</div>

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote `````)

````auto
``` 
   code goes here 
```

````

You can edit and correct your post by clicking the pencil ✏ icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

**Author:** ![jhamel](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@jhamel](https://discourse.nodered.org/u/jhamel)\
**Post date:** [16 January 2025 15:05 UTC](https://discourse.nodered.org/t/regex-test-alternate-between-true-and-false-notwithstanding-the-value-being-always-the-same/94678/3 "2025-01-16T15:05:14Z")

</div>

Just did, thanks

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [16 January 2025 15:12 UTC](https://discourse.nodered.org/t/regex-test-alternate-between-true-and-false-notwithstanding-the-value-being-always-the-same/94678/4 "2025-01-16T15:12:00Z")

</div>

I don't know but the regex should be `/\.pp[4567]$/`

---

<div class="post-metadata">

**Author:** ![jhamel](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@jhamel](https://discourse.nodered.org/u/jhamel)\
**Post date:** [16 January 2025 15:24 UTC](https://discourse.nodered.org/t/regex-test-alternate-between-true-and-false-notwithstanding-the-value-being-always-the-same/94678/5 "2025-01-16T15:24:30Z")

</div>

You are right, although, /.pp[4567]$/i works also. Don't get why the **g** seems to cause an issue but again, not a Regex expert.

thanks.

---

<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:** [30 January 2025 15:24 UTC](https://discourse.nodered.org/t/regex-test-alternate-between-true-and-false-notwithstanding-the-value-being-always-the-same/94678/6 "2025-01-30T15:24:32Z")

</div>

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