0

I'm getting a 500 in sapper (i think due to safari and firefox not support negative lookbehind assertions in js regex. But my try catch isn't stopping the problem.

import showdown from 'showdown';
const converter = new showdown.Converter({simplifiedAutoLink: true});

function parseContent(content) {ox()) && content.match(/http/)) {
    let parsed;    

    try {
         parsed = converter.makeHtml(content)
             .replace(/(?<!https?.+)\/?a\/([^\s]*)/g, ` <a href="/a/$1">a/$1</a>`)
             .replace(/\s*@(\w+)/g, ` <a href="/u/$1">@$1</a>`);
     } catch(err) {
         console.log(err);
         parsed = converter.makeHtml(content)
         .replace(/\s*@(\w+)/g, ` <a href="/u/$1">@$1</a>`);
    }

    return parsed;
}

export {
    parseContent  
}

How can I use a simpler regex for non-supporting browsers?

chovy
  • 72,281
  • 52
  • 227
  • 295
  • What is the regex supposed to do? – ggorlen Apr 20 '20 at 00:07
  • 2
    An invalid regex will be a **syntax** error, and as such it can't be caught by `try / catch` – Pointy Apr 20 '20 at 00:08
  • Turn `(?<!https?.+)` into a capturing group and make it optional, and use a callback method as a replacement argument to analyze the match to see if Group1 was matched, then use the right replacement. See [the answer](https://stackoverflow.com/a/641432/3832970). – Wiktor Stribiżew Apr 20 '20 at 00:09
  • Can you provide code given my example? – chovy Apr 20 '20 at 01:20

0 Answers0