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

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.

[{"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":[]}]

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 ```)

``` 
   code goes here 
```

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

See this post for more details - How to share code or flow json

Just did, thanks

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

1 Like

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.