# Support of "if" and "(?R)" in Regex

**URL:** https://es.discourse.group/t/support-of-if-and-r-in-regex/2084
**Category:** I have questions
**Created:** [July 11, 2024, 4:59pm UTC](https://es.discourse.group/t/support-of-if-and-r-in-regex/2084 "2024-07-11T16:59:16Z")
**Posts on this page:** 5
**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 11, 2024, 4:59pm UTC](https://es.discourse.group/t/support-of-if-and-r-in-regex/2084/1 "2024-07-11T16:59:17Z")

</div>

Why still no support for "if" and "(?R)" in Regex?

---

<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 11, 2024, 8:32pm UTC](https://es.discourse.group/t/support-of-if-and-r-in-regex/2084/2 "2024-07-11T20:32:52Z")

</div>

Hi @oleedd could you point to references from other languages that have these features? I'd be interested to hear what they are.

---

<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 12, 2024, 7:01am UTC](https://es.discourse.group/t/support-of-if-and-r-in-regex/2084/3 "2024-07-12T07:01:41Z")

</div>

Hi. It is probably the most known and useful what JavaScript doesn't have.

1. (?(condition)then|else). It is very old and seems to be not supported only in JavaScript.

2. 

> (?R) or (?0) tries to match the entire regex recursively. (?1), (?2), etc, try to match the relevant group.

> (?&name) tries to match the named group.
> 
> ```javascript
> regex.match(r"(Tarzan|Jane) loves (?1)", "Tarzan loves Jane").groups()
> ('Tarzan',)
> regex.match(r"(Tarzan|Jane) loves (?1)", "Jane loves Tarzan").groups()
> ('Jane',)
> 
> m = regex.search(r"(\w)(?:(?R)|(\w?))\1", "kayak")
> m.group(0, 1, 2)
> ('kayak', 'k', None)
> 
> ```
> 
> The first two examples show how the subpattern within the group is reused, but is _not_ itself a group. In other words, "(Tarzan|Jane) loves (?1)" is equivalent to "(Tarzan|Jane) loves (?:Tarzan|Jane)".
> 
> It’s possible to backtrack into a recursed or repeated group.
> 
> You can’t call a group if there is more than one group with that group name or group number ("ambiguous group reference").
> 
> The alternative forms (?P\>name) and (?P&name) are also supported.

It is a very known solution to match closing parenthesis and what is inside. I don't think it is problematic to implement because the engine just replaces "(?R)" with the whole regex patern, it just becomes bigger. And on backtracking, it should not remove those replacements.

More interesting new features here: [regex · PyPI](https://pypi.org/project/regex/)

---

<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 12, 2024, 9:16pm UTC](https://es.discourse.group/t/support-of-if-and-r-in-regex/2084/4 "2024-07-12T21:16:44Z")

</div>

JavaScript regex syntax is based on Perl, and the Perl docs are also quite searchable.

Both of these are existing Perl features: [https://perldoc.perl.org/perlre#(?(condition)yes-pattern|no-pattern)](https://perldoc.perl.org/perlre#(?(condition)yes-pattern%7Cno-pattern)), [https://perldoc.perl.org/perlre#(?PARNO)-(?-PARNO)-(?+PARNO)-(?R)-(?0)](https://perldoc.perl.org/perlre#(?PARNO)-(?-PARNO)-(?+PARNO)-(?R)-(?0)).

---

<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 14, 2024, 9:07pm UTC](https://es.discourse.group/t/support-of-if-and-r-in-regex/2084/5 "2024-07-14T21:07:08Z")

</div>

Because no one said about this? It is unlikely.  
JavaScript regex seems to be the most backward. Why so?
