---
description: 🚧 under construction
---

# alternation (or)

[JS](../../../..) ⟩ [value](../../..) ⟩ [object](../../../obj) ⟩ [regex](..) ⟩ [pattern]() ⟩ alternation (or)

{% hint style="success" %}
<pre class="language-javascript"><code class="lang-javascript">regex        matches
--------------------------
b(a|e|i)d    bad bed bid

(?(test)true)	        // If positive lookahead test matches, match true regex
(?(test)true|false)	// As above, otherwise match false regex
<strong>(?(capture)true)	// If capture (name or number) contains text, match true regex
</strong>(?(capture)true|false)	// As above, otherwise match false regex
</code></pre>
{% endhint %}

{% tabs %}
{% tab title="💈範例" %}
```javascript
(a|e|i)        // = [aei]

// quoting style: single (') -> double (")
let text = "'I'm the cook,' he said, 'it's my job.'";
text.replace(/(^|\W)'|'(\W|$)/g, '$1"$2'));
//            ╰─1──╯   ╰─2──╯ 
//  ⭐️ Groups that are not matched will be replaced by nothing.
// → "I'm the cook," he said, "it's my job."
```
{% endtab %}

{% tab title="📗 參考" %}
* [ ] [Regular Expression Quick Reference](http://regexrenamer.sourceforge.net/help/regex_quickref.html)
* [ ] Eloquent JS ⟩ Regular Expressions ⟩&#x20;
  * [ ] [Sets of Characters](https://eloquentjavascript.net/09_regexp.html#h_8EFR0DU1xd)
  * [ ] [The replace method](https://eloquentjavascript.net/09_regexp.html#h_k0YuTOu54D)
  * [ ] [Quoting Style](https://eloquentjavascript.net/09_regexp.html#i_dTiEW14oG0)
{% endtab %}

{% tab title="🗣 討論" %}
* [Regular Expressions: Is there an AND operator?](https://stackoverflow.com/a/67415321/5409815)
{% endtab %}
{% endtabs %}
