I have some text on an HTML page. Need a bookmarklet (no jQuery) that will find a segment of the text using regex, then replace it with a link with the text as a parameter
Example before:
aaa bbb ccc ddd
Example after:
aaa <a href="http:www.whatever.com?bbb">bbb</a> ccc ddd
assuming we were looking for "bbb"
This solution will crawl the DOM search for text nodes within the document elements, skipping any elements you want. For example, you probably want to skip <a> tags, as well as <script> tags and others. This way, you won't replace element nodes or essential page functionality.
Here it is minimized in bookmarklet form:
Copy that into a bookmark and try it out on any page! Note: search is case sensitive, but you can add the 'i' flag to the RegExp to prevent that.
Simplest regex:
Better:
Best:
The parentheses in regexp signify a matched group. The "$1" in the second argument is the first matched group.