Search function works very poorly in obsidian?

I am using the following Templater script to take over the global search used with CTRL+SHIFT+F.
With this I can find all compounds with word stems properly.

<%*
// Get the active workspace
const workspace = app.workspace;

// Ensure there's an active editor
const activeLeaf = workspace.activeLeaf;
if (!activeLeaf || !activeLeaf.view || !activeLeaf.view.sourceMode) {
    new Notice("No active editor found.");
    return;
}

// Get the editor instance
const editor = activeLeaf.view.sourceMode.cmEditor;

// Capture the current selection
let selection = editor.getSelection().trim();

// If no selection, cancel the search
if (!selection) {
    new Notice("No text selected.");
    return;
}

// Prepend '/' to the search term
let searchTerm = "/" + selection;

// Temporarily store the cursor position or selection range
const cursorPosition = editor.getCursor();

// URL-encode the search query
const encodedQuery = encodeURIComponent(searchTerm);

// Construct the Obsidian URI for the search
const uri = `obsidian://search?query=${encodedQuery}`;

// Create a temporary anchor to trigger the URI
const link = document.createElement('a');
link.href = uri;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);

// Restore the original selection after the script is executed
editor.setSelection(cursorPosition, cursorPosition);

// Optionally: Copy the search query to clipboard
navigator.clipboard.writeText(searchTerm).then(() => {
    new Notice(`Copied to clipboard:\n${searchTerm}`);
}).catch(err => {
    console.error('Could not copy text: ', err);
});
_%>

Usage: save then register script in Templater settings, bind it to the combination mentioned above then in your document, select text and hit key combo.

If your selection has special characters such as the following, you need to add a \ before them (manually edit the query in search bar) or (I forgot to add here before) use the second version below:

<%*
// Second iteration of script escapes special characters

// Get the active workspace
const workspace = app.workspace;

// Ensure there's an active editor
const activeLeaf = workspace.activeLeaf;
if (!activeLeaf || !activeLeaf.view || !activeLeaf.view.sourceMode) {
    new Notice("No active editor found.");
    return;
}

// Get the editor instance
const editor = activeLeaf.view.sourceMode.cmEditor;

// Capture the current selection
let selection = editor.getSelection().trim();

// If no selection, cancel the search
if (!selection) {
    new Notice("No text selected.");
    return;
}

// Function to escape special characters for regex search
function escapeSpecialCharacters(text) {
    return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}

// Escape special characters in the selection
let escapedSelection = escapeSpecialCharacters(selection);

// Prepend '/' to the search term
let searchTerm = "/" + escapedSelection;

// Temporarily store the cursor position or selection range
const cursorPosition = editor.getCursor();

// URL-encode the search query
const encodedQuery = encodeURIComponent(searchTerm);

// Construct the Obsidian URI for the search
const uri = `obsidian://search?query=${encodedQuery}`;

// Create a temporary anchor to trigger the URI
const link = document.createElement('a');
link.href = uri;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);

// Restore the original selection after the script is executed
editor.setSelection(cursorPosition, cursorPosition);

// Optionally: Copy the search query to clipboard
navigator.clipboard.writeText(searchTerm).then(() => {
    new Notice(`Copied to clipboard:\n${searchTerm}`);
}).catch(err => {
    console.error('Could not copy text: ', err);
});
_%>

Script also adds selection (query) to the clipboard.


Special Characters in Regex Example Usage
. [period] Matches any character except \n
^ Anchors to the start of a string
$ Anchors to the end of a string
* Matches 0 or more repetitions
+ Matches 1 or more repetitions
? Makes the preceding item optional
\ Escapes special characters
[] Matches any character in brackets
| Alternation (OR operator)
() Groups expressions
{} Specifies exact number of repeats
1 Like