2

Currently I have a use case where I need to grab the first path after '/#/' in url until '/'

// lookbehind and lookahead
'https://stackoverflow.com/questions/#/ask/anything-here'
    .match(/(?<=\/#\/).+(?=\/)/)
// ["ask" ...] I get word 'ask' in this case.

// supported in old browser
'https://stackoverflow.com/questions/#/ask/anything-here'
    .match(/\/#\/(.+)\//)
// ["/#/ask/", "ask", ...] I can get word 'ask' in res[1]

Im using positive lookbehind and positive lookahead here but as soon as I run unit test in CI/CD, it error because we are still using old browser version(Chrome 56) while my machine is using Chrome 76.

In caniuse website https://caniuse.com/#feat=js-regexp-lookbehind, Chrome start to support lookahead and lookbehind on version 62.

So my question is, it is possible to polyfill or ponyfill lookahead, lookbehind and name groups regex to old browsers.

Azriz
  • 3,122
  • 2
  • 12
  • 13
  • `#\/([^\/]+)\/` How about this ? – Oliver Hao Jul 30 '19 at 01:57
  • 1
    Do you even need to use a regular expression here? a couple of `split` calls would get you the word 'ask': `('https://stackoverflow.com/questions/#/ask/anything-here'.split('#', 2)[1]||'').split('/', 2)[1]` – Dean Taylor Jul 30 '19 at 01:59
  • If you wanted truly to polyfill, you'd need to modify [`RegExp.prototype[Symbol.match]`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@match), which I don't recommend. – Patrick Roberts Jul 30 '19 at 02:02
  • @PatrickRoberts I don't think a polyfill would be possible - regex literal syntax is not parsed via a modifiable function, right? In which case, the plain regex expression (before being passed to `.match`) would throw a `SyntaxError` while it's being parsed – CertainPerformance Jul 30 '19 at 02:06
  • @CertainPerformance that is true, yes. So hypothetically, a babel plugin that transforms regex literals with lookbehinds into a custom object with a `Symbol.match` method would be possible, but a polyfill would not. – Patrick Roberts Jul 30 '19 at 02:10
  • @DeanTaylor, ya sure we have a lot of option to solve this. But I was asking is it possible to polyfill/ponyfill lookbehind, lookahead, and name groups for regex. – Azriz Jul 30 '19 at 03:56
  • Years later, you can use a [Babel plugin](https://babeljs.io/docs/en/babel-plugin-transform-named-capturing-groups-regex) to make named capture groups work, but apparently not lookaheads and lookbehinds. – Patrick Stephansen Oct 28 '20 at 15:14

1 Answers1

-2

Why not simply use URL API and select hash then remove #/ from statrting

let url = 'https://stackoverflow.com/questions/#/ask/anything-here'


let valueAfterHash = (url)=>{
  let parsed = new URL(url)
  return parsed.hash.replace(/^#\//,'')
}


console.log(valueAfterHash(url))
console.log(valueAfterHash(`https://stackoverflow.com/questions/ask/anything-here`))

If you want to use perl falvour of regex, you can use xregexp library which supports a lot of functions which is not natively present in JS regex

Code Maniac
  • 37,143
  • 5
  • 39
  • 60