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.

1 Like

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)
2 Likes
#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

Thank you so much! Your code worked for me as is but I’m trying to tweak the regex input to convert a full date, like December 30, 2023 to 2023-12-30 format. Any ideas how this can be done? I’m not very familiar with regex, tried something like \w{3,9}?\s\d{1,2}?\s?,\s\d{4}? but got invalid escape error.

It’s a great question, but way beyond my regex skill level!

This can be done using Javascript’s Date object. Something like:

// #popclip
// name: Date to ISO
// icon: ISO
// language: javascript
// after: paste-result
const date=new Date(popclip.input.text);
return date.toISOString().slice(0, 10);

(The above block is an extension snippet — select it then click “Install Extension” in PopClip.)

Notes:

  • JS’s Date() constructor accepts an arbitrary string and will attempt to parse it as a natural language string.
  • toISOString() produces output like 2023-12-03T00:00:00.000Z
  • so we use string slice() to cut off the first 10 characters only to get the ISO date part.