Regex Match per action in extension (snippet)

I’m trying to solve a problem I have at work. We have multiple subdomains and I frequently need to test one URL on multiple subdomains. E.g. dev.example.com and local.example.com. I want an action that when I select a URL offers me just the right choices for the selected URL. I also want a 4th action that extracts part of the URL for me. So I tried the following:

# popclip
name: example.com URL
requirements: [urls]
regex: (https?:\/\/\w+\.example\.com.*)
icon: circle filled E
actions: 
  - title: E Local
    regex: (https?:\/\/\w+(?<!local)\.example\.com.*)
    javascript: |
	  let urls = popclip.input.data.urls;
	  urls = urls.map(url => url.replace(/https?:\/\/\w+(?<!local)\.example\.com/, 'http://local.example.com'));
	  popclip.pasteText(url.join("\n"), { restore: false })
  - title: E Dev
    regex: (https?:\/\/\w+(?<!dev)\.example\.com.*)
    javascript: |
	  let urls = popclip.input.data.urls;
	  urls = urls.map(url => url.replace(/https?:\/\/\w+(?<!local)\.example\.com/, 'https://dev.example.com'));
	  popclip.pasteText(url.join("\n"), { restore: false })
  - title: E Sandbox
    regex: (https?:\/\/\w+(?<!sandbox)\.example\.com.*)
    javascript: |
	  let urls = popclip.input.data.urls;
	  urls = urls.map(url => url.replace(/https?:\/\/\w+(?<!local)\.example\.com/, 'https://sandbox.example.com'));
	  popclip.pasteText(url.join("\n"), { restore: false })
  - title: workflow_id
    regex: (https?:\/\/\w+(?<!sandbox)\.example\.com.*)
    javascript: |
	  let urls = popclip.input.data.urls;
	  urls = urls.map(url => url.match(/\/(\d+\w+)\/?/)[0]);
	  popclip.pasteText(url.join("\n"), { restore: false })

I suspect, that in lieu of multiple actions, I need options. I can easily move the 4th action to its own one, but I only want the relevant options for the domains to appear if possible.

Any suggestions or hints are greatly received!

Hi @RosemaryOrchard I’m just taking a look … (I do like a challenge)

To help me understand what it’s trying to do can you give me an example input string and the corresponding output string ?

A few notes:

  • There’s tabs in the file for indentation so it’s invalid YAML, I can’t install it as-is.
  • I think the general approach of having different regex selectors is good and should work, although it might be even easier to collapse the first three actions into onw and handle the different cases into the code itself.
  • The overall regex at the top won’t do anything since it will be overriden by the regexes for each action. So it can be removed.
  • The regex syntax and some of the JS looks a bit suspect!

I think it could probably be done fairly simply with a few tweaks, but I’d just need to understand the requirements exactly to make a suggestion.

I have these exact same use cases - swapping out the subdomain and extracting part of the URL.

@RosemaryOrchard did you solve for this and if so, do you mind to share your extension? (also I love Automators, which is what turned me on to Popclip in the first place)

Thanks,
William