0

Is it possible using regex in JavaScript to match a string but only return part of the match?

For example in the string match collections/electronics/products but only return electronics

https://website.com/collections/electronics/products/some-product

This is what I have so far:

/(\/collections\/)(.*?)(\/products\/)/ig

Asa Carter
  • 2,207
  • 5
  • 32
  • 62
  • 1
    Explanations for all kinds of scenarios and example code to fetch one or multiple results is [here](https://stackoverflow.com/a/40782646/3832970). – Wiktor Stribiżew Apr 13 '18 at 09:01
  • I had to amend the JavaScript to return the second group: `var re = /(.*)\/collections\/(.*?)\/products\/(.*)/ig; var collection = location.href.replace(re, "$2");` – Asa Carter Apr 13 '18 at 09:44

0 Answers0