Note that there are some explanatory texts on larger screens.

plurals
  1. POReplace [url] tags with <a href> tags in Javascript/JQuery
    text
    copied!<p>I'm trying to use JQuery to achieve the following logic:</p> <ul> <li>Replace the string value of <code>[url="http://www.google.com"]Google[/url]</code> with <code>&lt;a href="http://www.google.com"&gt;Google&lt;/a&gt;</code></li> </ul> <p>Please see my HTML page below. The problem is that on pressing the button, the orignal text is just pasted and no RegEx replacements are made.</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head runat="server"&gt; &lt;title&gt;Test&lt;/title&gt; &lt;script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; //&lt;![CDATA[ function processJs() { var oldtext = $("#oldtext").html(); var newtext = oldtext.replace('\[url\s?=\s?"?(.*?)"?\](.*?)\[\/url\]', '&lt;a href="$1"&gt;$2&lt;/a&gt;'); $('#mydiv').html(newtext); } //]]&gt; &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;div id="oldtext"&gt; Try this funky new search engine: [url="http://www.google.com"]Google[/url] Or this older one from back in the day: [url="http://uk.altavista.com"]AltaVista[/url] &lt;/div&gt; &lt;div&gt; &lt;input type="button" id="btn" value="Replace" onclick="processJs(); return false;" /&gt; &lt;/div&gt; &lt;div id="mydiv" style="background-color: #eeeeee; border: 2px inset #aaaaaa"&gt; Replaced text will go here. &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>I've had this RegEx pattern work using ASP.NET, so I'm not sure where the problem lies when ported to JavaScript...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload