Reputation: 121
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 = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
var end_text = text.replace(/[&<>"']/g, function(m) { return map[m]; });
console.log(end_text);
end_text = end_text.replace(/<a href="([\/a-zA-Z\s]+)"(\stitle="[a-zA-Z0-9\s-]+")?(\sclass="[a-zA-Z0-9\s-]*")?>([a-zA-Z\s-]+)<\/a>/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="go-go-go">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
Reputation: 4474
Your regex does exactly what you asked it: the $2
and $3
are replaced by themselves, i.e. including "
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 = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
var end_text = text.replace(/[&<>"']/g, function(m) { return map[m]; });
console.log(end_text);
return end_text.replace(/<a ([\s\S]*?)>([a-zA-Z\s-]+)<\/a>/g,
function(match, tag, content) {
if (/title="[\s\S]*home[\s\S]*"/.test(tag)) {
return match;
}
return '<a ' + tag.replace(/"/g, '"') + '>' +
content.replace(/"/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