Snippet for Generic Search & Replace

Hi
I see another extension here “Remove Vertical Line(s) - #2 by nick”, which is very similar

Can v make a generic extension with the option to search & replace

Concept snippet, tho JS needs to be looked at. Cheers

# popclip
name: SearchNreplace
icon: Search filled dot
options:
  - identifier: Search
  label: Search Name
  type: string
options:
  - identifier: Replace
  label: Replace Name
  type: string
javascript: |
  const SearchName = encodeURIComponent(popclip.options.Search)
  const ReplaceName = encodeURIComponent(popclip.options.Replace)
  popclip.pasteText(popclip.input.text.replace(/\{SearchName}/g, ‘{ReplaceName}'))
description: Searches and replaces string
1 Like

Concept snippet

And it’s a great concept @luckyearl - welcome to the forum.

Here I have knocked it into some kind of shape:

# popclip
name: SearchNreplace
icon: Search filled X
macos version: '10.15' # for String.replaceAll
options:
- { identifier: Search, label: Search Name, type: string }
- { identifier: Replace, label: Replace Name, type: string }
javascript: popclip.pasteText(popclip.input.text.replaceAll(popclip.options.Search, popclip.options.Replace))
description: Searches and replaces string

CleanShot 2022-05-07 at 07.26.07@2x

1 Like

Thank you Nick for looking at it. works great

1 Like

Whether this replaced fragment can be multiple, separated by “,”

Challenge accepted, welcome to the forum @epoxzk !

# popclip
name: SearchNreplacePRO
icon: Search filled X!
macos version: '10.15' # for String.replaceAll
description: Searches and replaces multiple terms
options:
- { identifier: Search, label: Search terms, type: string, description: Comma separated. }
- { identifier: Replace, label: Replacements, type: string, description: Comma separated. }
javascript: |
  const search = popclip.options.Search.split(',')
  const replace = popclip.options.Replace.split(',')
  if (search.length !== replace.length) throw new Error('Unequal number of terms')
  const zipped = search.map((k, i) => [k, replace[i]])
  let text = popclip.input.text
  for (const [s, r] of zipped) {
    text = text.replaceAll(s, r)
  }
  popclip.pasteText(text)

CleanShot 2022-05-17 at 09.21.12

Great, the idea seems to developing

But how do you search & replace “,” :wink:

Change the separator :slight_smile:

Also cud further develop it as search & replace a string rather than a character :slight_smile:
Cheers

That’s perfect, its so easy to create a new extension, genius

That already works, you can put strings in betwee the commas. My example just happened to be single characters.
CleanShot 2022-05-17 at 11.35.13@2x
You can also set the replacement to nothing to delete the things. e.g. one,,three will delete ‘is’ and replace this and fine.

Tx Nick, I shud have tried.
Have a gud day

1 Like

I love this extension but have one question: is it possible to specify a new line character as part of the replacement string?

I don’t think so, since the text field is a single-line field so you can’t put an enter character in.

So there’s no escape sequence, e.g., ‘/r’ or ‘/n’?