0

Example of string:

<a href="#" data-html="<input type="text" data-html="<input type="text">">"></a>

The target:

<a href="#" data-html="<input type=&quot;text&quot; data-html=&quot;<input type=&quot;text&quot;>&quot;>"></a>

I tried:

var string = '<a href="#" data-html="<input type="text" data-html="<input type="text">">"></a>';

string.replace(/"+(.?)"+/g, function(s) {
  return '"' + s.slice(1, s.length).slice(0, -1).replace(/"/g, '&quot;') + '"';
});

Such as I'm noob with regex I need your help :)

P.S. we can't use lookahead & lookbehind (javascript)

Alexander
  • 65
  • 4
  • There is lookahead: x(?=y) or lookbehind x(?<=y) in the JavaScript. Not everywhere lookbehind works, so try [here](http://stackoverflow.com/questions/7376238/javascript-regex-look-behind-alternative) for alternative. – Evil Nov 19 '16 at 21:34

1 Answers1

2

At first glance I would suggest to use the escape() function on the string inside your data-html="" value. This handles all conflicting html characters. Then when you read the vale from data-html you can use unescape().

See the following link that explains escape() and unescape(): http://www.w3schools.com/jsref/jsref_unescape.asp

Edit: The unescape() function was deprecated in JavaScript version 1.5. Use decodeURI() or decodeURIComponent() instead: http://www.w3schools.com/jsref/jsref_decodeuri.asp

Example:

var strDataHtml_1 = encodeURI('<input type="text">')
var strDataHtml_2 = encodeURI('<input type="text" data-html="' + strDataHtml_1 + '">');
var string = '<a href="#" data-html="' + strDataHtml_2 + '"></a>';
WizzKidd
  • 196
  • 8