Follow

javascript: the cursed language that keeps on giving 

developer.mozilla.org/en-US/do

> JavaScript RegExp objects are stateful when they have the global or sticky flags set (e.g., /foo/g or /foo/y). They store a lastIndex from the previous match. Using this internally, test() can be used to iterate over multiple matches in a string of text (with capture groups).

Which means that if you do normal programmer things like putting `const regex = /.../g;` above several functions that use the same constant regex, you get this Extremely Useful and Exciting Behavior:

```
> re = /maybe/g
/maybe/g
> re.test('maybe')
true
> re.test('maybe')
false
> re.test('maybe')
true
> re.test('maybe')
false
```

javascript: the cursed language that keeps on giving 

@emily oh shit. this definitely means some past-jobs code of mine I've shipped have, uh, bugs 😬

GOAT language truly

Sign in to participate in the conversation
Mastodon

The social network of the future: No ads, no corporate surveillance, ethical design, and decentralization! Own your data with Mastodon!