0

I have written a very comprehensive search function for a website I am building and after completing the build figured out that Safari doesn't allow for positive lookbehinds. I don't have time to be angry or wait for them to allow this so need a replacement snippet that will do the same functionality.

After reading a lot of similar articles I am still stumped as to how it is achieved.

I would say im like a 5/10 on RegExr so if you could explain it in plain english that would really help.

Here is one of my many snippets, grabbing the value between a variable and a tailing character, then collecting the value from the array created and replacing this array:

var myTermReg = new RegExp('(?<=' + queryTermIdentifier + '\=).*?(?=\\+)', 'g');
var termString = queryString.match(myTermReg);
termString = termString[0];

The '?<=' is not accepted yet by Safari, which is breaking my whole application. Can someone please help me write this out in a Safari safe RegExr snippet?

I appreciate there are articles of similar issues, I have looked at them and cannot figure this out so would really appreciate if someone can help explain what the direct replacement is using this snippet as an example.

How do I look between two substrings without using a lookbehind?

EDIT (Example data requested in comment):

I have a URL for example: https://www.example.co.uk/?phrase=+category=ceramics+tag=aesthetic,corrosion-resistant+relation=AND

I need to be able to see what variables have been inserted into the URL such as a Category: category=ceramics

As my code is ran it will look between the phrase category and the trailing '+':

var myCategoryReg = new RegExp('(?<=' + category + '\=).*?(?=\\+)', 'g');

Then create a value using match and reassigning the value to the variable.

Jason Is My Name
  • 929
  • 2
  • 14
  • 38
  • Hi Jason, can you show us the actual problem you are trying to solve here, by way of sample data? Most likely, there is some workaround which doesn't even use lookarounds. – Tim Biegeleisen May 11 '20 at 17:15
  • of course, let me update my question. – Jason Is My Name May 11 '20 at 17:16
  • 1
    All you need is a capturing group. `var myTermReg = new RegExp(queryTermIdentifier + '(.*?)\\+', 'g');`. Or, better, `var myTermReg = new RegExp(queryTermIdentifier.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') + '([^+]*)', 'g');`. Then just grab Group 1 value. – Wiktor Stribiżew May 11 '20 at 17:17
  • @TimBiegeleisen - Question updated with an example – Jason Is My Name May 11 '20 at 17:21
  • @WiktorStribiżew - does that return an array? Are you able to explain just a little into the components of them lines? – Jason Is My Name May 11 '20 at 17:24
  • 1
    Regex never returns an array, the method does, see [the answer](https://stackoverflow.com/a/40782646/3832970) with code at the bottom. – Wiktor Stribiżew May 11 '20 at 17:29
  • @WiktorStribiżew If I use your suggested better line I get a returned value of: /relation([^+]*)/g . The method I was previously using would return a value like so: AND. I appreciate your method may work, probably better than how I was doing it in the first place but I just need it manipulating so it will return what I need please. – Jason Is My Name May 11 '20 at 17:30
  • 1
    Use the capturing group instead of the lookbehind, the code and example use is present in my linked answer. – Wiktor Stribiżew May 11 '20 at 17:33
  • I think I have understood whats going on. Appreciate the reference, I couldn't find that answer in my search prior to writing this question. – Jason Is My Name May 11 '20 at 17:35

0 Answers0