you need to wrap the tag а all occurrences on the regular schedule, but do not replace it in the ready-made tags а wrote the following

 function replaceURLsInMessage(text){ text = text.replace(/(?<![">])(http(s)?:\/\/)?(www\.)?([-a-zA-Z0-9@:%._\+~#=]{2,256}\.(?!gif|png|jpg)[az]{2,6}\b)([-a-zA-Z0-9@:%_\+.~#?&//=]*)(?!["<])/g, '<a href="$&" target="_blank">$&</a>'); return text; } 

First js swears on the regular season

secondly, the regular page does not work if inside the tag а link is written in the middle of the text.

the second is okay, the priority is the first item

I enclose an example of the text

 fadsgfads <a href="vk.com" target="_blank">Смотрите в vk.com подробнее </a> фывафывафыва вафывафыва афвыафыва м vk.com фывафыва 

the call is made as follows by clicking on the button

 $this.html(replaceURLsInMessage($this.html())) 

as a result, I expect to see the following

 fadsgfads <a href="vk.com" target="_blank">Смотрите в vk.com подробнее </a> фывафывафыва вафывафыва афвыафыва м <a href="vk.com" target="_blank">vk.com</a> фывафыва 

that is, the first link is left untouched, and the second - the one that was the text, became the link, the link in href and inside the tag itself

  • This is one of the reasons why you should not use regulars when working with Html :-) - Grundy
  • how do you call this function and add examples of text that can be transmitted - Grundy
  • I call by clicking on save, like $ this.html (replaceURLsInMessage ($ this.html ())) - as an example - sivik_xes
  • The code is also in question. And replace the button? or how? - Grundy
  • one
    @ WiktorStribiżew, Replacing links in a contenteditable div - Grundy

3 answers 3

If you want to use a regular expression, you can use exciting submissions and analyze captured texts inside an anonymous method, and then decide what to do with them.

 $("div").each(function() { $(this).html($(this).html().replace(/<a\s+[^<]*?>[^<]*<\/a>|((?:https?:\/\/)?(?:www\.)?([-a-zA-Z0-9@:%._+~#=]{2,256}\.(?!gif|png|jpg)[az]{2,6}\b)[-a-zA-Z0-9@:%_+.~#?&\/=]*)/ig, function($0, $1) { return $1 ? '<a href="' + $1 + '">' + $1 + '</a>' : $0; })); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div contenteditable="true">fadsgfads <a href="vk.com" target="_blank">Смотрите в vk.com подробнее </a> фывафывафыва вафывафыва афвыафыва м vk.com фывафыва</div> 

Those. using <a\s+[^<]*?>[^<]*<\/a> [^ <] *?> <a\s+[^<]*?>[^<]*<\/a> you can catch already ready links with tags, and with the help of your expression (but already in (...) ) you can find links without tags. If submask # 1 is found ( $1 ? ), You need to add tags. If not, just insert back the full match $0 .

  • one
    IT, thank you so much - sivik_xes

Javascript does not support backward regular expression. By this (?<![">]) Leads to

First js swears on the regular season

  • Maybe there are some options, the main result)) does not support, does not support, now it became clear - sivik_xes

Apparently, the error at the very beginning: what kind of unshielded question mark at the very beginning, right after the opening bracket? I do not remember this syntax in Javascript. If you meant exactly the “?” Character, and not the regular operator, then escape it:

 text.replace(/(\?<![">])(http(s)?:\/\/)?(www\.)?([-a-zA-Z0-9@:%._\+~#=]{2,256}\.(?!gif|png|jpg)[az]{2,6}\b)([-a-zA-Z0-9@:%_\+.~#?&//=]*)(?!["<])/g, '<a href="$&" target="_blank">$&</a>'); 

Now the regular season does not spit out an error. How she is working - to judge you. If you give an example of a complex text and describe what you want - we add to the regular list.

  • An example is given, the condition also, in view there was certainly not a question mark, please read the condition more carefully - sivik_xes
  • @sivik_xes read, 5 times already. "js swears on the regular season" in your priority. I pointed out the mistake he swears at. Now that you’ve completed the answer, it’s clearer. An interesting task ... - Ivan Pshenitsyn
  • I'm sorry, but what of this condition was not clear to you? you need to wrap the tag and all occurrences on the regular schedule, but do not replace it in the ready-made tags and wrote the following - sivik_xes
  • @sivik_xes it was not clearly clear what "regular entry" is. Your supplement has clarified the situation. - Ivan Pshenitsyn