# "o" flag for regex to allow overlapped results

**URL:** <https://es.discourse.group/t/o-flag-for-regex-to-allow-overlapped-results/2090>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [July 21, 2024, 8:22am UTC](https://es.discourse.group/t/o-flag-for-regex-to-allow-overlapped-results/2090 "2024-07-21T08:22:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![oleedd](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/oleedd/32/2148_2.png) [@oleedd](https://es.discourse.group/u/oleedd)\
**Post date:** [July 21, 2024, 8:22am UTC](https://es.discourse.group/t/o-flag-for-regex-to-allow-overlapped-results/2090/1 "2024-07-21T08:22:57Z")

</div>

"caratraany".match(/\wa\w/og) // "car", "rat", "raa", "aan"

---

<div class="post-metadata">

**Author:** ![lightmare](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/lightmare/32/843_2.png) [@lightmare](https://es.discourse.group/u/lightmare)\
**Post date:** [July 22, 2024, 6:47am UTC](https://es.discourse.group/t/o-flag-for-regex-to-allow-overlapped-results/2090/2 "2024-07-22T06:47:47Z")

</div>

What's the use case? What would be the benefit of the "o" flag over doing this with existing facilities?

A) shifting match start:

```javascript
pattern = /\wa\w/g
input = "caratraany"
while (m = pattern.exec(input)) {
  console.log("match:", m)
  pattern.lastIndex = m.index + 1
}

```

```javascript
match: ['car', index: 0, input: 'caratraany', groups: undefined]
match: ['rat', index: 2, input: 'caratraany', groups: undefined]
match: ['raa', index: 5, input: 'caratraany', groups: undefined]
match: ['aan', index: 6, input: 'caratraany', groups: undefined]

```

B) capturing look-ahead assertion:

```javascript
pattern = /(?=(\wa\w))./g
input = "caratraany"
for (const m of input.matchAll(pattern)) {
  console.log("match:", m)
}

```

```javascript
match: ['c', 'car', index: 0, input: 'caratraany', groups: undefined]
match: ['r', 'rat', index: 2, input: 'caratraany', groups: undefined]
match: ['r', 'raa', index: 5, input: 'caratraany', groups: undefined]
match: ['a', 'aan', index: 6, input: 'caratraany', groups: undefined]

```

---

<div class="post-metadata">

**Author:** ![oleedd](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/oleedd/32/2148_2.png) [@oleedd](https://es.discourse.group/u/oleedd)\
**Post date:** [July 22, 2024, 7:36am UTC](https://es.discourse.group/t/o-flag-for-regex-to-allow-overlapped-results/2090/3 "2024-07-22T07:36:48Z")

</div>

It is not that. The result should be just `["car", "rat", "raa", "aan"]`. You have to add much code to join it so. And with "o", it could be in 2 formats depending on "g".  
And your B) is a workaround trick what is not very good.  
The benefit is very short and clear syntax. Many new features may be polyfilled. And what, don’t add anything that may be polyfilled?  
Regex engines should progress. For some reason only JavaScript regex stands still.  
For example, `startsWith()` was added despite many existing easy ways to do what it does.

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [July 22, 2024, 8:13am UTC](https://es.discourse.group/t/o-flag-for-regex-to-allow-overlapped-results/2090/4 "2024-07-22T08:13:12Z")

</div>

> [@oleedd](#):
>
> Regex engines should progress. For some reason only JavaScript regex stands still.

New features are being added to Regex in JavaScript. The ES2024 spec added support for the `v` flag:

> **[Ecma International approves ECMAScript 2024: What’s new?](https://2ality.com/2024/06/ecmascript-2024.html#regular-expression-flag-%2Fv)**
>
> On 26 June 2024, the 127th Ecma General Assembly approved the ECMAScript 2024 language specification, which means that it’s officially a standard now. This blog post explains what’s new.

---

<div class="post-metadata">

**Author:** ![oleedd](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/oleedd/32/2148_2.png) [@oleedd](https://es.discourse.group/u/oleedd)\
**Post date:** [July 22, 2024, 8:21am UTC](https://es.discourse.group/t/o-flag-for-regex-to-allow-overlapped-results/2090/5 "2024-07-22T08:21:57Z")

</div>

Ok, it is good. I just google what new features and don't find anything. But it is only Unicode, not a new search ability for regular text.

---

<div class="post-metadata">

**Author:** ![Josh-Cena](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/josh-cena/32/1408_2.png) [@Josh-Cena](https://es.discourse.group/u/Josh-Cena)\
**Post date:** [July 22, 2024, 3:13pm UTC](https://es.discourse.group/t/o-flag-for-regex-to-allow-overlapped-results/2090/6 "2024-07-22T15:13:50Z")

</div>

The first one is not a good one because it's not aware of surrogate pairs. Real Unicode regexes may advance `lastIndex` by 2 if the last index was a surrogate pair. (It's fine here because the pattern us not Unicode, but it doesn't generalize.)

The second one is fine but it changes both the regex and the shape of the match and can only be called a workaround, not a proper solution.
