Error Parsing regex

Hi,

I have this command to extract the domain from the URL

{
      "Command": "executeScript_Sandbox",
      "Target": "const regex = /https?:\\/\\/(?:www\\.)?([^\\/]+).*/;\nconst str = ${!URL};\nconst subst = `$1`;\n\n// The substituted value will be contained in the result variable\nconst result = str.replace(regex, subst);\n\nreturn result;",
      "Value": "baseUrl",
      "Description": ""
    },

but executing it I receive this error that I never saw before:

  • [error] [Line 10]: Unexpected token (1:22)

I have to change something?

The updated _sandbox version of executeScript only supports ES5 level JS, so no “const”.

Two solutions:

  • Use executeScript ( the non-sandbox version)

  • Replace “const” by “var”

This macro works and shows both solutions:

{
  "Name": "const",
  "CreationDate": "2022-6-13",
  "Commands": [
    {
      "Command": "open",
      "Target": "https://forum.ui.vision/t/error-parsing-regex/10270",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "executeScript",
      "Target": "const regex = /https?:\\/\\/(?:www\\.)?([^\\/]+).*/;\nconst str = ${!URL};\nconst subst = `$1`;\n\n// The substituted value will be contained in the result variable\nconst result = str.replace(regex, subst);\n\nreturn result;",
      "Value": "baseUrl1",
      "Description": ""
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "var regex = /https?:\\/\\/(?:www\\.)?([^\\/]+).*/;\nvar str = ${!URL};\nvar subst = \"$1\";\n\n// The substituted value will be contained in the result variable\nvar result = str.replace(regex, subst);\n\nreturn result;",
      "Value": "baseUrl2",
      "Description": "ES5 JS => do not use \"const\""
    },
    {
      "Command": "echo",
      "Target": "${baseURL1}",
      "Value": "green",
      "Description": ""
    },
    {
      "Command": "echo",
      "Target": "${baseURL2}",
      "Value": "blue",
      "Description": ""
    }
  ]
}

See also:

2 Likes