Snippet: Replace Slash with Dash or Period

A request I received by email:

I use TextExpander and copy dates frequently and have numerous expands, including dates in this format: 10/30/2022. When I save a file with the “/” included, it gives windows user problems, and I re-format it to a “.” or a“ -” instead. -

Is there a chance a Replace Slash between words with Dash , and vice versa, and a Replace Slash between words with a Period could be considered?

My Solution
This one is fairly easy with a JavaScript action:

#popclip
name: Slash Replace
after: paste-result
actions:
- title: Slash to Dash
  icon: square filled monospaced /-
  javascript: return popclip.input.text.replace(/\//g, '-')
- title: Slash to Period
  icon: square filled monospaced /.
  javascript: return popclip.input.text.replace(/\//g, '.')

(The above block is an extension snippet - select it to install the extension with PopClip)

Technical Notes

  • This example demonstrates how you can put two actions in one snippet.
  • Note that paste-result applies to both actions by virtue of being defined at the top level.
  • We have to backslash-escape the forward slash in the regex /\//.

Demo
CleanShot 2022-11-01 at 12.09.33

1 Like

I always use hyphens for that purpose – some systems have trouble with dots in a file name.

This is terrific. But dare I ask for something that would be absolute BRILLIANT? What about something would convert the order to ISO 8061 YYYY-MM-DD?

So, in that case, 10/30/2022 would become 2022-10-30. There would need to be a version for US (MM/DD/YYYY) and the rest of the world (DD/MM/YYYY), but presumably that could be an option. This woudl be really useful!

1 Like
#popclip
name: US Date to ISO
icon: circle filled MDY
after: paste-result
javascript: |
  const regex = /(\d{2})\/(\d{2})\/(\d{4})/g
  const replacer = (_, m, d, y) => `${y}-${m}-${d}`
  return popclip.input.text.replace(regex, replacer)

I’ll leave the rest-of-world version as an exercise for the reader :slight_smile:

Actually, for ISO 8601, the next-to-last line needs to be year, month, day: ${y}-${m}-${d}

#popclip
name: US Date to ISO
icon: circle filled MDY
after: paste-result
javascript: |
  const regex = /(\d{2})\/(\d{2})\/(\d{4})/g
  const replacer = (_, m, d, y) => `${y}-${m}-${d}`
  return popclip.input.text.replace(regex, replacer)
1 Like
#popclip
name: EURO Date to ISO
icon: circle filled DMY
after: paste-result
javascript: |
  const regex = /(\d{2})\/(\d{2})\/(\d{4})/g
  const replacer = (_, d, m, y) => `${y}-${m}-${d}`
  return popclip.input.text.replace(regex, replacer)

:netherlands: Joel :netherlands:

1 Like