2

I'm seeing some very strange behaviour with a little script I'm writing (and running with Node v6). If I place the following line in a script, say index.js:

console.log((/autonom.*/ig).test('autonomy'))

and run it with node index.js, I'm seeing false. Unexpected!

Stranger still is that if I open an interactive shell with node, and run:

> (/autonom.*/ig).test('autonomy')
true

I get true, as expected. Is there any reason why the code in the script would have a different effect to that in the repl?

Edit the regex had a little bit of spice for no real reason. I see the same behaviour even with the trivial:

console.log((/^autonomy$/).test('autonomy'))

Edit the plot thickens hugely:

console.log((/autonom\S*/i).test('autonomy'))
console.log((/autonom\S*/i).test('autonomy'))

node index.js with this file prints:

true
false
Jack Preston
  • 3,824
  • 1
  • 11
  • 8
  • Remove the `g` modifier. `console.log(/autonom.*/i.test('autonomy'))`. See http://stackoverflow.com/questions/1520800/why-regexp-with-global-flag-in-javascript-give-wrong-results – Wiktor Stribiżew Nov 09 '16 at 16:51
  • @WiktorStribiżew no joy: even console.log((/^autonomy$/).test('autonomy')) prints false... – Jack Preston Nov 09 '16 at 16:52
  • Note the brackets - `console.log(/autonom.*/i.test('autonomy'))` – Wiktor Stribiżew Nov 09 '16 at 16:54
  • All of the examples return true in both scripts and repl for me (at least with node v6.9.1). – mscdex Nov 09 '16 at 16:55
  • Both return `true` for me.. Node v7.1.0 , Do you have any third party modules your loading with the script version?? Oh, and Chrome return true too, to complete the circle.. :) – Keith Nov 09 '16 at 16:57
  • This is very strange... if I save the file around a bit, copy - paste it from here, change node version in nvm, I sometimes see a true... Is there some kind of regex cache?! – Jack Preston Nov 09 '16 at 17:04
  • Starts to sound like [data rot](https://en.wikipedia.org/wiki/Data_degradation). – robertklep Nov 09 '16 at 18:05

1 Answers1

0

I've finally got to the root of the problem, and thought I'd share an answer to the question!

Why might Node be behaving strangely in this situation?

The problem lies in where i took my original strings from - which happened to be to copy and paste from a PDF article. Let's take the string 'autonomy' for an example.

In Atom, my editor, this looked exactly like 'autonomy'. As it did when I pasted it to SO in the original question.

However, if I take the one in-editor and output it as a hex buffer, I see:

6175746f6e6f6d01

For comparison, the string 'autonomy', if typed out manually, is:

6175746f6e6f6d

Herein lies the problem. The reason it worked for all of you lot, and the reason it worked for me in the REPL, is that most of the places I pasted it (SO, bash, ...) didn't understand / threw out that final 01, leaving you seeing the "proper" string 'autonomy' as it looks.

FYI, the ASCII character 01 is SOH: Start of Header. SHRUG!

Jack Preston
  • 3,824
  • 1
  • 11
  • 8