9

I was genuinely baffled by this until I read up more on regex flags and realised that this is the intended behaviour😂

Comments
  • 4
    wait wtf?
  • 1
    Where is written that an regex is true and false?
  • 0
    What? In the goddamn fuck?
  • 0
    this doesnt seem to be regex' fault.
    correct me if im wrong, but i think the regex pattern per se is between / and /.
    if anything i think its the "gi" flag in the end which does that weird behaviour. which would be implementation-specific (makes me think of sed regex)
    i dont use regex very often, so please do correct me if im wrong
  • 1
    @bad-frog
    g = global it means that it tests all lines
    i = case insensitive
  • 1
    @stop so its like in sed. ok, i get it this far.

    but then what could possibly indicate that you are expecting alternating results? and even more importantly, what would be the use of having your results alternate?

    people talk about c like its some arcane, ancient magic, but low level is actually a piece of cake compared to these high level languages' methods mumbo jumbo
  • 3
    g is global.

    First call of test.

    First match.

    Second call of test.

    Second match or False if not found.

    Third call of test.

    Reset as previous match -> False, first match of test.

    It's obvious....

    And maybeeeeeee reading docs and utilizing variables would make this even more obvious.
  • 1
    @IntrusionCM
    tx for the enlightenment.
    you might want to count the number of occurencies of a certain pattern and getting false tells you you arrived at the end of the string.
    its obvious when you think of it that way.
    had to think of it that way tho...
  • 1
    @bad-frog :)

    It's one of many reasons I hate JS so much.

    The whole thing is a complete brain fucking shit.

    It would be far more logical (and is implemented like this in many languages) if you either pass a reference in of a container datatype which gets filled with matches (e.g. PHP) or getting an object back which allows to access the matched groups (e.g Python).

    But to allow a function call to behave like a generator while not being a "true" generator is fuckity.

    Like a lot of APIs in JS. :)

    My sassy ness was the result of having fun with this a long time ago. JS APIs are truly a marvelous cluster fuck.

    I was pretty angry at that time and still am cause some dev thought it would be great to use this in a while loop fashion in a function. I'm still getting murderous intents remembering that.
  • 1
    @IntrusionCM wow. they made javascript to look like c and be a full oop language, but they ended up with the worst of both worlds. and worse than that for this one: this method follows pure functionnal logic.

    and now i have to learn this bullshit bc all our frontend belongs to java. *facepalm_9000
  • 1
    @bad-frog

    I don't think this is functional at all.

    A generator would e.g. stop yielding results after reaching end of iteration.

    An iterator would throw an exception when trying to access an out of range element.

    That's what makes me furious.

    It could have done that.

    But no. They start over, at the first element instead

    Just think how funny e.g. for loops would be which automatically restart.

    :@
  • 1
    @IntrusionCM lmao yeah, i see exactly what you mean.

    i was thinking functionnal because how would you implement something like that in a functionnal style? : a while loop, calling a function returning a boolean, with a counter.
    no structure needed whatsoever

    exactly the same logic as the dev had behind this method. to be used in a while loop and a counter. (lol oop ur methods are woke functions)

    what makes me wonder now, is how arrays work in java and what would be the behaviour of that method is if the string is changed between two calls... bc when i think about it, all this sounds inefficient as frig
  • 1
    @bad-frog strings can only be replaced. Arrays have an counter which saves the amount of items.
Add Comment