{"id":6667,"date":"2022-08-06T16:20:45","date_gmt":"2022-08-06T07:20:45","guid":{"rendered":"https:\/\/the-world-star.com\/?p=6667"},"modified":"2026-11-19T14:00:20","modified_gmt":"2026-11-19T05:00:20","slug":"how-to-reuse-strings-in-wordpress-search-regex-replacements","status":"publish","type":"post","link":"https:\/\/the-world-star.com\/en\/server\/how-to-reuse-strings-in-wordpress-search-regex-replacements-6667\/","title":{"rendered":"How to Reuse Strings in WordPress 'Search Regex' Replacements"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">I had a hard time figuring out how to reuse strings when doing replacements with the WordPress 'Search Regex' plugin, but after some research, I finally got it to work.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Almost no sites explain this, and the one site I did find had misinformation. The official Search Regex documentation doesn't mention it either. I wondered if it used the same syntax as PHP, tested it out, and it worked, so I'm writing it down as a note. Does nobody else use this? Is PHP syntax just common knowledge? Maybe I struggled because I'm too used to regex replacements using `query-replace-regexp` in Emacs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once you know how, it's simple. In Emacs, you wrap patterns in `( ~~~~ )` and reuse them with `1`, where `` acts as the special escape key.<br>In PHP and the WordPress 'Search Regex' plugin, you don't need the ``. You just wrap them in `( ~~~~ )`. (Though you might need to escape literal parentheses like `(` if you're matching them). To reuse the captured group, you use `$1`.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;a href=\"https:\/\/www.facebook.*&gt;~~~~~&lt;img src=\"http~~~~\/&gt;&lt;\/a&gt;\n\u307f\u305f\u3044\u306a\u306e\u3092\u524d\u5f8c\u306e&lt;a ~&gt; &lt;\/a&gt;\u3092\u9664\u3044\u3066\u771f\u3093\u4e2d\u306e\n&lt;img src=\"http~~~~\/&gt;\n\u3060\u3051\u3092\u6b8b\u3057\u305f\u3044\u3068\u304d\u3001\n\n\u691c\u7d22:&lt;a href=\"https:\/\/www.facebook.*&gt;<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">(<\/mark>&lt;img .*\/&gt;<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">)<\/mark>&lt;\/a&gt;\n\u7f6e\u63db:$1\n\u3068\u3059\u308b\u3002<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"930\" height=\"686\" src=\"https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/08\/06161517\/Screenshot-from-2022-08-06-15-56-05-e1659770202935.png\" alt=\"\" class=\"wp-image-3240\" srcset=\"https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/08\/06161517\/Screenshot-from-2022-08-06-15-56-05-e1659770202935.png 930w, https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/08\/06161517\/Screenshot-from-2022-08-06-15-56-05-e1659770202935-300x221.png 300w, https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/08\/06161517\/Screenshot-from-2022-08-06-15-56-05-e1659770202935-768x567.png 768w\" sizes=\"auto, (max-width: 930px) 100vw, 930px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Something like this.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is super convenient\u2014even if you leave the replacement field blank, running a search lets you find specific articles and text.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"903\" height=\"686\" src=\"https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/08\/06161727\/Screenshot-from-2022-08-06-15-56-50-e1659770264582.png\" alt=\"\" class=\"wp-image-3241\" srcset=\"https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/08\/06161727\/Screenshot-from-2022-08-06-15-56-50-e1659770264582.png 903w, https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/08\/06161727\/Screenshot-from-2022-08-06-15-56-50-e1659770264582-300x228.png 300w, https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/08\/06161727\/Screenshot-from-2022-08-06-15-56-50-e1659770264582-768x583.png 768w\" sizes=\"auto, (max-width: 903px) 100vw, 903px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Furthermore, if you put `$1` in the replacement field, it will output the matched text as-is.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"913\" height=\"674\" src=\"https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/08\/06161833\/Screenshot-from-2022-08-06-15-57-38-e1659770340416.png\" alt=\"\" class=\"wp-image-3242\" srcset=\"https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/08\/06161833\/Screenshot-from-2022-08-06-15-57-38-e1659770340416.png 913w, https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/08\/06161833\/Screenshot-from-2022-08-06-15-57-38-e1659770340416-300x221.png 300w, https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/08\/06161833\/Screenshot-from-2022-08-06-15-57-38-e1659770340416-768x567.png 768w\" sizes=\"auto, (max-width: 913px) 100vw, 913px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What is the practical use case for this? You can copy text and images from past Facebook posts directly in your browser, paste them into a new WordPress post, and easily build a nice-looking article. However, the images will still be hosted on Facebook, meaning clicking them takes users back to Facebook. Removing those links is the first step of the cleanup process. The images look fine as they are for now, but eventually, I'll upload them to the media library and use Search Regex to replace those URLs too!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I had a hard time figuring out how to reuse strings when doing replacements with the WordPress 'Search Regex' plugin, but after some research, I finally got it to work. Almost no sites explain this, and the one site I did find had misinformation. The official Search Regex documentation doesn't mention it either. I wondered [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2606,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"en_US","_original_post":"https:\/\/175.41.248.22\/?p=3239","_lightning_design_setting":{"layout":"default"},"footnotes":""},"categories":[3],"tags":[279,283,1233,1234,1235,1236,67,278],"class_list":["post-6667","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-emacs","tag-query-replace-regexp","tag-search-regex","tag-reuse","tag-string-reuse","tag-replacement","tag-server","tag-php","en-US"],"_links":{"self":[{"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/posts\/6667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/comments?post=6667"}],"version-history":[{"count":1,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/posts\/6667\/revisions"}],"predecessor-version":[{"id":6668,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/posts\/6667\/revisions\/6668"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/media\/2606"}],"wp:attachment":[{"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/media?parent=6667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/categories?post=6667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/tags?post=6667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}