javascript - search and replace with jQuery -


I want to search a keyword in the body and want to change it with a link if it is not linked from anywhere is. My code is:

  var search = $ ('body'). Html (); Search = search Location (/ jQuery / g, function ($ 1) {return ('& lt; a href = "http://jquery.com">' + $ 1 + '& lt; / a & gt;') ;}); . $ ('Body') HTML (search);  

The problem is that it takes the place of all keywords, even if it is already connected.

I do not want to change or change it already.

> Someone can suggest me what to do ....

You should search the jQuery selectors to search for links.

  $ (document) .ready (function () {var link, search; $ links = $ ('a [href = http: //jquery.com]'); if ($ Links.length) {search = $ ('body') .html (). Replace (/ jQuery / g, function ($ 1) {return ('& lt; a href = "http://jquery.com" Gt; '$ 1 +' & lt; / a & gt; ';)}; $ (' body '). Html (search);}});  

If you want to replace only on the first instance of "jQuery", then delete the g modifier from your regex.


Comments