RegExp: Negative lookbehind and Negative lookahead doesn't work in Custom JavaScript on AppPreview

In my Custom JavaScript, I need to dynamically replace strings in texts, so it encodes HTML formatting. I’m using Custom JavaScript for that.

Below is the code inside JS node:

try {
  const ex = "I am what I am."
  const kw = "am";
  const t = '(?<![aA-zZ])'+kw;
  const y = new RegExp(t,'g');
  const ex_html = ex.replace(y, "<strong>am</strong>")
  return [0, { result: ex_html }]
} catch (err) {
  const error = {
    code: 'unknownError',
    message: 'Something went wrong.',
    rawError: err,
  }
  return [1, {result: error}]
}

Here’s the result I get when pressing the button JS result.

The code works on others platforms. Refer to the link below to see the code in action.
https://es6console.com/kta6u02x/

Please, @Mevi, @Harri_Sarsa or other appGyver team, would take a look into this and say if it is something that will persist after the app is built. In this case I’ll look up for other solution.

Hi @Alan_Alves! I added some console.log statements to the code (you can see them in the debugger’s console when running the app in mobile) and it errors at new RegExp(t,'g'); When running the snippet in web preview however, the same code works as expected. I’ll ask our developers what’s going on here, will also include your other thread.

1 Like

Hey!

Lookaheads have been supported in JS for a good while, but lookbehinds are a relatively new addition. Support depends on the runtime, so web works if you’re using a compatible browser (e.g. Chrome), but won’t work if you’re using Safari. This includes the formula preview in the editor (other thread). The mobile runtime currently does not support them, and this includes standalone builds.

To be brutally honest, I’d advise to try and find an alternative solution for now. We’ll look into upgrading the mobile JS runtime, but that’s likely quite an involved process and part of a major release type of thing.

Meanwhile I’ll see if we’d be able to get some sort of warning in for the formula preview. Might be a bit tricky, but you shouldn’t have to know about JS runtime intricacies beforehand when writing formulas. Custom JS is a different situation, but in this case the root cause is the same for both.