Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS Regex lookbehind not working in firefox and safari

I have this following regex which is working in chrome but causes an error in firefox or safari. I need to modify it to make it work. Can anybody help out a poor soul? Thanks in advance!

regex: /(?=<tag>)(.*?)(?<=<\/tag>)/

Basically, I have to match any char in between <tag> and </tag> and need to retain both tags. I used this expression as an argument to array.split.

input: "The quick brown <tag>fox</tag> jumps over the lazy <tag>dog</tag>"

operation: input.split(regex)

output: ["The quick brown ", "<tag>fox</tag>", " jumps over the lazy ", "<tag>dog</tag>"]

like image 842
webedward Avatar asked Oct 19 '19 04:10

webedward


1 Answers

firefox and safari doesn't have support for lookbehind yet, you can use capture group ( used so that this the pattern on which we are splitting will be also be added in output ) and split on <tag> </tag>

let str = "The quick brown <tag>fox</tag> jumps over the lazy <tag>dog</tag>"

let regex = /(<tag>.*?<\/tag>)/

console.log(str.split(regex).filter(Boolean))
like image 72
Code Maniac Avatar answered Sep 23 '22 21:09

Code Maniac