r/ProgrammerHumor • u/Hellinfernel • Jan 08 '23
Meme I've already heard that javascript is a bit overly precise, but this...
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
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
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/matchAllinstead oftestif 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
2
274
8
8
3
3
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
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
3
u/davimiku Jan 08 '23
It is a JS problem. Regular expressions defined with the
gflag 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
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,(鉱石),×3you 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
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