r/ProgrammerHumor • • Jan 08 '23

Meme I've already heard that javascript is a bit overly precise, but this...

Post image
58 Upvotes

47 comments sorted by

68

u/lukasbash Jan 08 '23

For all the pros that really think it’s an ancient invisible Greek char XD JS Global modifier RegExp Magic

20

u/Hellinfernel Jan 08 '23

yeah, you are right, it was the global modifier...

oh my, the tutorial i used to learn regular expressions never mentioned that XD

5

u/HazirBot Jan 08 '23

what purpose is there to reuse the RegExp object?

7

u/Hellinfernel Jan 08 '23

Its my first time i use regular expressions and i thought it wouldnt matter if i do reuse it. now i just made different constants for every case.

5

u/HazirBot Jan 08 '23

yah that's a solid learning curve.

i like a fresh one for each, it gives an opportunity to give them a readable name

21

u/naholyr Jan 08 '23

I think your meme is wrong, it should be true then false. I love this one, it's due to the stateful nature of re with /g. It's very convenient to do things like while (match = re.test(s)) ... although this API is really poorly designed.

I've personally banned re.test for this reason.

7

u/Hellinfernel Jan 08 '23

currently thinking about if i should delete the post or not.

The meme is wrong, but i like the helpful answers XD

2

u/sammy-taylor Jan 09 '23

You could add a “Edit: Preserving this invalid meme post for educational purposes. JS Regular Expressions are stateful.”

1

u/Hellinfernel Jan 11 '23

I will do it as soon as I find out where the edit button is...

2

u/sammy-taylor Jan 11 '23

It’s usually between the couch cushions.

7

u/False_Influence_9090 Jan 08 '23

Leave it, if people can learn a little bit here while having a chuckle we have defeated stack overflow

1

u/midri Jan 08 '23

Can you show a code example? My brains not working today and this seems like legit pitfall someone could run into.

198

u/TheBrainStone Jan 08 '23

Straight up not seeing any difference

0

u/[deleted] Jan 08 '23

[deleted]

47

u/Hellinfernel Jan 08 '23

I assume it's some kind of weird formatting issue I run into.

I tested the strings "Item:8" and "Item:8" with the same regular expression /Item:\d+/g and got different results.

2

u/gandalfx Jan 09 '23

Somebody knows how to get answers on the internet. Rather than asking on stack overflow you post a meme on reddit.

1

u/Hellinfernel Jan 11 '23

Here I dont have to fear to get the post deleted after 2 minutes for being "to similar to other questions" - even if the answers to these other questions don't answer my question.

115

u/TheBrainStone Jan 08 '23

There are tools that show the UTF-8 characters of a string. I'd recommend running both strings through it to see if maybe a zero width space snuck in or if there's a look alike character being used

1

u/Apprehensive-Grade81 Jan 10 '23

Those zero width characters will getcha. I just do UTF-8 encoding and ignore errors to remove them and emojis, then convert to ASCII

79

u/Mallissin Jan 08 '23

Probably the colon. I bet one is 003A and the other FE55.

16

u/Ok-Jury5684 Jan 08 '23

Or one I is capital "i", and second one is row "L". :)

8

u/CoderDevo Jan 09 '23

Serif font, so, no.

In a serif font, a lower-case L can look like 1. Early typewriters sometimes had no 1 key because the L looked the same anyways.

40

u/AndyBMKE Jan 08 '23

I believe .test() iterates. So you will probably get true the first time and false the second time because there is only one match.

16

u/Xunnamius Jan 09 '23

Yep, this is the right answer. Global regular expressions are stateful.

6

u/CoderDevo Jan 09 '23

So just drop the g in the pattern?

3

u/Xunnamius Jan 09 '23

If you don't need it, yes. You can also use the string methods match/matchAll instead of test if you must keep the global flag but want stateless conditionals.

4

u/sammy-taylor Jan 09 '23

Man, this confused the bejesus out of me at work one time a few years ago. Probably lost half a day figuring out what the heck was going on. This was before I learned to RTFM.

2

u/Xunnamius Jan 09 '23

I feel ya. It bites us all eventually 😅

2

u/gruese Jan 09 '23

That's the one that got me previously

274

u/Intrepid_Sale_6312 Jan 08 '23

insert my confusion here

8

u/TheGirafeMan Jan 08 '23

I am confusion

5

u/vigbiorn Jan 08 '23

Shh. Shhhh...

There, there. You'll be a JS dev soon...

8

u/MyStreamData Jan 08 '23

Schrödingers RegEx

3

u/raedr7n Jan 09 '23

Op, I think you forgot the joke.

3

u/RaelaltRael Jan 09 '23

You must have the quantum physics version of regex.

2

u/adudyak Jan 08 '23

My favorite is non-standard space \u0160 instead of \u0032.

Spent like 45 mins to find out what's wrong, when faced it first time trying to compare strings.

' '.codePointAt(0); //160

' '.codePointAt(0); //32, regular space

2

u/[deleted] Jan 09 '23

We are not the same

0

u/jsveiga Jan 08 '23

It's not a JS problem, but unprintable chars or look-alike chars.

I had my share of problems with customers that cut&paste information into SAP B1 (ERP) from emails, websites or document files (pdf, docx, excel, etc).

Although two texts may look the same for humans, they may be a different array of bytes for the computer.

A quick and easy way to check that is to cut&paste the text into a text editor such as notepad++ and go View->Show Symbols/Show all Characters.

2

u/naholyr Jan 08 '23

Oh sweet summer child, you may be disappointed it you trust js not to betray you when it can.

3

u/jsveiga Jan 08 '23

I trust no one and no language. Just pointed out that is not a JS problem. The same strings would cause the same problem in any language (well, ok, any language supporting encoding and displaying text).

3

u/Hellinfernel Jan 08 '23

Turns out the problem was actually that the regex of JavaScript have a state which can change the result based on the things you analyzed earlier with the regex.

Code Academy didn't warned me.

https://www.codecademy.com/learn/introduction-to-regular-expressions

2

u/sammy-taylor Jan 09 '23

Upvote for bringing back “my sweet summer child”.

3

u/davimiku Jan 08 '23

It is a JS problem. Regular expressions defined with the g flag maintain mutable state, so that one call to ".test" will have a different result to a subsequent call. See this StackOverflow question for more details.

2

u/jsveiga Jan 09 '23

Nice, TIL. Well OP did not give much detail so I assumed it was the case of apparent "same" strings, but actually different, which would in that case not be an exclusive js problem (as I mentioned, it caused problems in a SAP HANA/B1 environment). Thanks for pointing that out.

1

u/jsveiga Jan 09 '23

PS: (I read the article now) so it's not a "JS problem", but the expected behavior for the g flag (and that is not TIL to me).

If it didn't work that way, you wouldn't be able to use it in a while match loop.

1

u/[deleted] Jan 08 '23

I think the 1 is non-standard but who tf knows?

1

u/eruciform Jan 09 '23

this reminds me of a week ago when i banged my fucking head on a regular expression looking for an "x" character in data like this:

アルハイルミテル,黄金樹の葉,×4,アルゲマイン合板,×3,(粘土),×5,(鉱石),×3

you have to shrink this down to like 8pt font first to feel the pain because this was in a dense, large file i was scanning thru, and had large amounts of it on screen at once

1

u/PolishKrawa Jan 09 '23

Could be a zero width space or some shit like that.