Nik
Nik

Reputation: 121

How decode link from string?

In javascript we return row with replaced symbols. If row have link, not need replace symbols for link.

For this we use code:

function Html(text) {
    var map = {
        '&': '&',
        '<': '&lt;',
        '>': '&gt;',
        '"': '&quot;',
        "'": '&#039;'
    };

    var end_text = text.replace(/[&<>"']/g, function(m) { return map[m]; });
    console.log(end_text);
    end_text = end_text.replace(/&lt;a href=&quot;([\/a-zA-Z\s]+)&quot;(\stitle=&quot;[a-zA-Z0-9\s-]+&quot;)?(\sclass=&quot;[a-zA-Z0-9\s-]*&quot;)?&gt;([a-zA-Z\s-]+)&lt;\/a&gt;/g,'<a href="$1"$2$3>$4</a>');
    //<a href="$1"$3>$4</a>

    return end_text;
}

var str = '<a href="/homepage/test" title="go-go-go">go-go-go</a>';
console.log(Html(str));

At first we replace symbols in row, and than we are trying to decode the link in string. But we get problem: we get <a href="/homepage/test" title=&quot;go-go-go&quot;>go-go-go</a> instead of <a href="/homepage/test" title="go-go-go">go-go-go</a>.

Where error and whether it is possible to improve the code?

Upvotes: 0

Views: 335

Answers (1)

cFreed
cFreed

Reputation: 4474

Your regex does exactly what you asked it: the $2 and $3 are replaced by themselves, i.e. including &quot;s!

So I don't see any easy way to solution to achieve what you want this way.

Otherwise you might do something like this:
(edited: now includes a test to keep encoded entities when title includes "home", according to the OP's comment below)

function Html(text) {
  var map = {
    '&': '&amp;',
    '<': '&lt;',
    '>': '&gt;',
    '"': '&quot;',
    "'": '&#039;'
  };

  var end_text = text.replace(/[&<>"']/g, function(m) { return map[m]; });
  console.log(end_text);
  return end_text.replace(/&lt;a ([\s\S]*?)&gt;([a-zA-Z\s-]+)&lt;\/a&gt;/g,
    function(match, tag, content) {
      if (/title=&quot;[\s\S]*home[\s\S]*&quot;/.test(tag)) {
        return match;
      }
      return '<a ' + tag.replace(/&quot;/g, '"') + '>' + 
      content.replace(/&quot;/g, '"') + '</a>';
    }
  );
}

console.log(Html( // should result with plain HTML
  '<a href="/homepage/test" title="go-go-go">go-go-go</a>'
));
console.log(Html( // should result keeping encoded entities
  '<a href="/homepage/test" title="go-home">go-go-go</a>'
));

Upvotes: 2

Related Questions