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
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
4
Whether this replaced fragment can be multiple, separated by “,”
nick
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
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
13
I love this extension but have one question: is it possible to specify a new line character as part of the replacement string?
nick
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
15
So there’s no escape sequence, e.g., ‘/r’ or ‘/n’?