Search and Replace with Regex [SOLVED]

Even with ([^=])==([^=]) it does not find anything.

Well, I’m stumped.

Just one final check, if you’re using VS Code: is the tiny regex toggle (in the right side of the search box) activated? I sometimes forget about it and vscode understandably just does a literal search for my expression.

You can also check out this regex101 saved search and do some experiments:

Keep me posted. We’ll figure this out.

1 Like

Ah yes, that was it, it was not enabled. Yes, now it finds the files.

It found many more instances tahn when I just did == for find. I have not gone through all the results, but I found one where it identified the ==, even added ** in the pane showing the effect, but it does not show it to change.

For most/all others it shows:

Might be to do with the comma after “mistake”?

The [^=] bits should match anything but an equal sign and the $1 and $2 should replace it with whichever character that happens to be.

I sometimes have a hard time interpreting VS Code’s replacement preview. Maybe it will perform fine when doing the actual find/replace. If it doesn’t, let me know; I’d be curious about why it doesn’t work.

It did a number of them, but not all. Here is an example:

This one is similar but partially done:

When I do my simplistic search, i.e. ==, it finds almost half as many again as the Regex expression, incl. the ones above.

:face_with_monocle: I can’t think of any reason for that difference. I’ll do some testing on my Mac later and get back to you.

Please don’t do too much, you undoubtedly have more important matters to attend to.

So, after I ran the S & R on VSC with your Regex, I came across instances where the == had not been replaced. I just came across this:

It actually added **. So, I’ll run my simplistic S&R and see what happens. If there are still == remaining I’ll ask for help again.

Yeah, I forgot about those cases where there’s nothing before or after the ==. Sorry about that, and thanks for the update.

Edit 2020-10-12

I’ve tried a different approach and this seems to work rather well in VS Code:
Find: ==([^=].+?[^=])==
Replace: **$1**

Just one stupid question:

are you talking about the Obsidian built-in replace function, which is located in the editor and only works for a single file?

I thing that replace function does not support Regex

@GLight: it’s not a stupid question, you’re just asking for clarification :grinning:

The question was for a global search and replace. I am aware of the fact that Obs does not have the Global Replace bit, which is why I had to do it through a 3rd party, in this case I used VS Code.

For the sake of completeness I can report that my simplistic, non-Regex way worked, although I had to run the function twice, for some unknown reason.

Thanks for asking the question because you made me realise this thread had not fully been fully completed.

It’s really a nice question, since it made me realize my mistake. Obsidian doesn’t support global regex find and replace, so my answer was misleading. I always use VS Code for that.

@GLight @Klaas There is a feature request for global search and replace.

1 Like

@sam.baron: cheers, I was aware of that one and had already hearted it. While we await possible implementation we need a workaround.

You can probably use a regex search and replace on the page to remove all markdown links.

For example regex101: Remove Markdown Links

Thanks. I am not into regex, I find it too complex to get a handle on, although I admit it would be a very useful tool to have in my toolbox.

But, if I am not mistaken, there are also different flavors of regex, which complicates it further.

I have used VS Code’s global search & replace on Obsidian successfully for my use cases.

I’m in the same boat. Maybe somebody can chime in or ask for help on discord.

Also, it seems the search/replace feature within a note does not support regex, for that you would need a separate plugin

I got it working.
Install the obsidian-regex-replace plugin.
Run the command “regex find/replace” and find \[([^\[]+)\](\(.*\)) and replace: $1

and it will remove external links in the note

in the note but not in all the notes, which is what the OP was about.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.