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
nick
May 7, 2022, 6:08am
2
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
1 Like
Thank you Nick for looking at it. works great
1 Like
epoxzk
May 15, 2022, 11:29pm
4
Whether this replaced fragment can be multiple, separated by “,”
nick
May 17, 2022, 8:22am
5
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)
Great, the idea seems to developing
But how do you search & replace “,”
Also cud further develop it as search & replace a string rather than a character
Cheers
That’s perfect, its so easy to create a new extension, genius
nick
May 17, 2022, 10:35am
10
That already works, you can put strings in betwee the commas. My example just happened to be single characters.
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
nello
August 31, 2022, 3:17am
13
nick:
name: SearchNreplace
I love this extension but have one question: is it possible to specify a new line character as part of the replacement string?
nick
September 5, 2022, 10:01pm
14
I don’t think so, since the text field is a single-line field so you can’t put an enter character in.
nello
September 5, 2022, 11:00pm
15
So there’s no escape sequence, e.g., ‘/r’ or ‘/n’?