How can I search for regular expressions within webpages using Google Chrome or IE?

73

18

How can I search for regular expressions like 'foo|bar' on webpages using Google Chrome or IE? I'm aware of the "Regular Expression Searcher" extension, but it does not work for me. (Nothing happens when I hit slash.) The reviews on the web store indicate that this is a common problem for many users.

Sarah

Posted 2012-04-27T21:58:18.190

Reputation: 731

1

You don't use Firefox, but you might find one of its add-ons interesting: deeper web http://deeperweb.com/ It doesn't do REs, but it does assist finer grained searching. If you're really desperate, you could always save the source of a web page and then search it with a utility or editor that does support REs, but that would be pretty ugly.

– Joe – 2012-04-30T18:41:58.470

maybe use NotePad++ as external editor for web pages. There you can search for regexps. – alfred – 2012-05-11T14:53:21.453

Answers

40

Using Javascript to match regular expressions

Maybe you want try this at chrome's console:

var p=/.*(regu).+?\ /gi; console.log( document.body.innerText.match(p) );

Just open console, copy and paste above to console and hit enter. You can test it here at this page.
This can be improved if it fits in.

Here we print to console match indexes and matched text. Here we try to match text that contains regu, 20 chars before (or less if start of line) and 10 chars after (or less if eol).

var p=/.{0,20}regu[^ \n]+[^\n]{0,10}/gi;
while (m = p.exec(document.body.innerText)) { 
    console.log( 'Index: '+m.index+' Match: '+m ); }

Also try this, it will paint background of all matches on page red, rexexp is not perfect but at least it should not mess with HTML tags:

var p=/(\>{1}[^\n\<]*?)([^\n\<]{0,30}regu[^\n\<]{0,10})/gi,b=document.body;
b.innerHTML=b.innerHTML.replace(p,'$1<span style="background-color:red;">$2</span>');

Bookmarking this:

Another way to use this is through javascript: protocol (same code as just above):

javascript:(function(){var p=/(\>{1}[^\n\<]*?)([^\n\<]{0,30}regu[^\n\<]{0,10})/gi,b=document.body;b.innerHTML=b.innerHTML.replace(p,'$1<span style="background-color:red;">$2</span>');})();

For example, using javascript: protocol one can insert a little search box to any web page for searching regexp's.
I think that you already know that simple regexp can also used to remove red matches from page.
If I continue to develop this for few more hours we may have search plugin that fit in bookmark :)

Sampo Sarrala - codidact.org

Posted 2012-04-27T21:58:18.190

Reputation: 2 268

1Some previous versions of chrome do not support innerText. It is recommended to check if innerText is supported and default to textContent if innerText is not usable. – Spencer D – 2015-11-25T20:14:18.323

Your initial code for finding elements can be with code [...document.body.innerText.matchAll(p)] for 73+ version of Chrome. – Vadim Ovchinnikov – 2019-05-15T08:25:10.167

6

Regular Expression Search from Google Chrome.

A simple search utility that allows you to search a web page using regular expression.

Features under development

  • toggle through found results
  • improved UI
  • toggle between case sensitivity
  • create shortcut for toggling the extension
  • Allow pressing "Enter" key when searching (instead of clicking on search button)

Important Notes
This is a page action extension so it won't work right away on the page you have already opened. I recommend restart your browser before start using this extension. You can also try to refresh your opened page.

user

Posted 2012-04-27T21:58:18.190

Reputation: 1 853

4

The link appears unavailable. Is it the same extension as Chrome Regex Search?

– Ruslan – 2019-01-29T05:32:30.763

4I don't have a better suggestion, but this extension doesn't work very well. While enabled, AdBlock doesn't block all ads. Also, after searching this page, for example, the add comment stops working. – Dennis – 2012-04-29T03:21:00.633

3

The Find+ extension has good reviews and has not been mentioned yet: https://chrome.google.com/webstore/detail/find%20-regex-find-in-page/fddffkdncgkkdjobemgbpojjeffmmofb?hl=en-US

I tried it out, and it works very well and has a clear, simple UI.

It can search text across HTML elements. The others that I tried were unable to do that.

enter image description here

twasbrillig

Posted 2012-04-27T21:58:18.190

Reputation: 377

3

The other extensions that were mentioned didn't work for me, so I wrote my own open source chrome extension that you can try out here: https://chrome.google.com/webstore/detail/regex-search/bcdabfmndggphffkchfdcekcokmbnkjl?hl=en&gl=US

Source code is available on github: https://github.com/gsingh93/regex-search

gsingh2011

Posted 2012-04-27T21:58:18.190

Reputation: 863

1

In Chrome Developer Tools mode, type ctrl + shift + F (on Windows, not sure about other OS).

There's a reg-ex option for searching. This will search all files, so it will include JavaScript source. I'm not sure (how) you can limit it to only HTML.

Fuhrmanator

Posted 2012-04-27T21:58:18.190

Reputation: 2 712

1

In Chrome on macOS, hold Alt+Command+I to open Developer Tools, select the HTML source code for the page (under the "Sources" tab), hold Command+F to search, and select the .* (Regex) symbol.

If you need to search only text, you can use pandoc to convert HTML to plain text:

$ pandoc https://superuser.com/questions/417875 -t plain -o tmp.txt

And then use less to search with Regex (search with /).

gmarmstrong

Posted 2012-04-27T21:58:18.190

Reputation: 141

0

I would like to suggest this Chrome plugin.

ultraon

Posted 2012-04-27T21:58:18.190

Reputation: 101

I think you are suggesting this as a solution, so I edited it. Also you can provide a link without displaying the full link. You click on the chain link icon which is visible when you are typing your answer. – KAE – 2018-09-12T12:12:28.400

0

not really a search, but I found this regexp highlighter (that I found somewhere) quite useful, and works for all browsers.

javascript:(function(){var%20count=0,%20text,%20regexp;text=prompt(%22Search%20regexp:%22,%20%22%22);if(text==null%20||%20text.length==0)return;try{regexp=new%20RegExp(%22(%22%20+%20text%20+%22)%22,%20%22i%22);}catch(er){alert(%22Unable%20to%20create%20regular%20expression%20using%20text%20'%22+text+%22'.\n\n%22+er);return;}function%20searchWithinNode(node,%20re){var%20pos,%20skip,%20spannode,%20middlebit,%20endbit,%20middleclone;skip=0;if(%20node.nodeType==3%20){pos=node.data.search(re);if(pos%3E=0){spannode=document.createElement(%22SPAN%22);spannode.style.backgroundColor=%22yellow%22;middlebit=node.splitText(pos);endbit=middlebit.splitText(RegExp.$1.length);middleclone=middlebit.cloneNode(true);spannode.appendChild(middleclone);middlebit.parentNode.replaceChild(spannode,middlebit);++count;skip=1;}}else%20if(%20node.nodeType==1%20&&%20node.childNodes%20&&%20node.tagName.toUpperCase()!=%22SCRIPT%22%20&&%20node.tagName.toUpperCase!=%22STYLE%22){for%20(var%20child=0;%20child%20%3C%20node.childNodes.length;%20++child){child=child+searchWithinNode(node.childNodes[child],%20re);}}return%20skip;}window.status=%22Searching%20for%20%22+regexp+%22...%22;searchWithinNode(document.body,%20regexp);window.status=%22Found%20%22+count+%22%20match%22+(count==1?%22%22:%22es%22)+%22%20for%20%22+regexp+%22.%22;})();

ceiling cat

Posted 2012-04-27T21:58:18.190

Reputation: 3 557

I should have scrolled down before I did it myself... https://pastebin.com/VA9xvmM8

– Sebi – 2019-10-08T19:59:27.673

All the UUencoded chars makes it hard to read, I found what appears to be the multiline version at http://stackoverflow.com/questions/9280195/differences-between-two-regex-javascript-bookmarklets

– Alok – 2012-05-02T02:09:14.840

1

I use the regexp highlighter from this site and it works fairly well for me http://dpatrickcaldwell.blogspot.in/2010/09/regular-expression-search-bookmarklet.html

– user – 2013-03-07T14:00:32.737

I needed the normal (not bookmarklet) JS (for use in Chrome extension), and solution from @buffer link worked fine – Evgeniy Dolzhenko – 2014-01-12T22:30:50.313

-1

Solomon Ucko

Posted 2012-04-27T21:58:18.190

Reputation: 101

This doesn't work when I put it in the URL bar. Would be cool though if it did. – Ahmed – 2016-01-06T01:13:36.447

It also removes formatting :( – Solomon Ucko – 2016-03-06T23:16:27.160

Also, the javascript: portion won't get pasted into the URL bar in Chrome. – Solomon Ucko – 2016-03-06T23:18:17.207

1It's remains only to add somewhere DROP TABLE.. ;-)) – Drakonoved – 2018-02-19T07:43:01.210