Plugin: Find and replace

Name

Find and replace

Icon

find_replace

Instructions

{
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);
}
}
}
}