Using the Search Regex plugin

Search Regex provides strong functionality and has no good alternatives, so even though it was reported as “untested”, it was worth a try. This article is the “missing manual” that shows you how to use it.

General Usage Notes

  • “Replace” is really “Preview”, while “Replace and Save” makes changes
  • Says you can double click text in the replace-preview to edit it, but
    doesn’t seem to work. Support forum says it hasn’t worked for 3 years, at least.
  • The view/edit buttons, meanwhile apply to the entire post, and erase the search.
    So if you click them, open them in a new tab.
  • The bottom row of checkboxes makes a regex search. Any character but [\^$.|?*+() is supposed to be a literal, but many non-alphanumeric characters are interpreted by the plugin as special characters, including > and /.
    To prevent those syntax errors, delimit the search with @ characters at the start and end.
  • Once you put in the @ characters, you must select one of the options, like “case insensitive”. Otherwise, the search says it didn’t find anything.
  • Available at Tools > Search Regex

Specific Uses

  • Adding target=”_blank” attributes to links that don’t have it.
    • The goal is find all external links that don’t already have target=”_blank”, and add it.
      Here is the search pattern: @(href=”https://[^3][\S]*”)>@

      • [^3] matches any character but “3” (the first char of my temporary URL)
      • .*? is a pattern that prevents a “greedy” match, that spans everything it can on a line, but it’s still too greedy. It matches …href=”…” target=”…. The workaround is to use [\S]*, instead, which matches any non-whitespace character.
      • The @ delimiters are needed in this plugin to prevent characters after the closing paren from throwing an “Unknown modifier” error.

Issues

  • Non-greedy matching is still a bit too greedy.
    The .*? pattern prevents truly egregious matches that span the whole line, and ending on a completely unrelated quotation mark, but the match is still a tad too greedy. The intent is to ignore links that already have the target=”_blank” attribute, but the .*? pattern matches strings like these, instead of stopping at the first end-quote, the way you’d like:

    <a href=”https://…” target=”_blank”

    Workaround:
    Instead of searching for .*?, search for [\S]*, to match anything but a space.

Copyright © 2017, TreeLight PenWorks

Please share!

1 Comment

    Trackbacks & Pingbacks

    1. Converting to WordPress: Benefits and Drawbacks | Treelight.com March 30, 2017 (8:23 am)

      […] Note: I use the Search Regex plugin. It hasn’t seen any development in a while, so it’s reported as “untested”. But there were no reasonable alternatives, so I gave it a try. Once I figured out its idiosyncracies, it has worked great. (For more, see Using the Search Regex plugin.) […]

    Add your thoughts...

    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    Categories


    %d bloggers like this: