javascript Regular Expression

Regex Matching and Group Matching in JavaScript

A memo about regex matching and group matching in JavaScript. document.location.href.match(/https://(google|yahoo)(.*)/)</code>&lt;/pre&gt;To extract the group match that would contain google or yahoo from here...

Shou Arisaka
1 min read
Nov 9, 2025

A memo about regex matching and group matching in JavaScript.

Example

document.location.href.match(/https:\/\/(google|yahoo)(.*)/)

To extract the group match that would contain google or yahoo from here,

document.location.href.match(/https:\/\/(google|yahoo)(.*)/)[1]

It can be extracted as an array. It’s hard to understand if you don’t know it, as it’s not stored in some variable nor passed to a block like in Ruby.

Share this article

Shou Arisaka Nov 9, 2025

🔗 Copy Links