Blog
Log in
Plugin: Find and replace
Published by
Lucian
September 28, 2023
Last changed
about 1 year ago
views
Name
Find and replace
Icon
find_replace
Instructions
open_in_new
open_in_new
{
noteOption
:
{
"🔎"
:
async
function
findAndReplace
(
app
,
noteUUID
)
{
try
{
let
results
=
await
app
.
prompt
(
"What would you like to replace?"
,
{
inputs
:
[
{
label
:
"Text to find"
,
type
:
"text"
,
}
,
{
label
:
"Text to replace"
,
type
:
"text"
,
}
,
]
,
}
)
;
let
textToFind
=
results
[
0
]
;
let
textToReplace
=
results
[
1
]
;
let
noteHandle
=
await
app
.
findNote
(
{
uuid
:
noteUUID
}
)
;
// let noteContents = await noteHandle.content();
let
noteContents
=
await
app
.
getNoteContent
(
{
uuid
:
noteUUID
}
)
;
console
.
log
(
noteHandle
.
uuid
)
;
// await app.alert(noteContents);
let
replaceRegex
=
new
RegExp
(
textToFind
,
'g'
)
;
let
newNoteContents
=
noteContents
.
replace
(
replaceRegex
,
textToReplace
)
;
await
app
.
replaceNoteContent
(
noteHandle
,
newNoteContents
)
;
}
catch
(
err
)
{
await
app
.
alert
(
err
)
;
}
}
}
}