0

I want to me able to replace all {{text}} occurrences by OOOOO.

This code works except the replacement.

Could you please help me with that ?

Thanks.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{{Titre}} de la pub</title>
</head>

<body>
Contenu et {{compagnie}}
</body>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script>
    var a = $("html").html().match(new RegExp("\{\{[^{}]+\}\}", "g"));
    for (i = 0; i < a.length; ++i) {
        $("html").html().replace(a[i], 'OOOOO');
    }
</script>
</html>
  • 4
    I think this qualifies for Tony the Pony! You generally shouldn't parse the entire document with a regex. – adeneo Dec 15 '14 at 21:07
  • [**TO͇̹̺ͅƝ̴ȳ̳ TH̘Ë͖́̉ ͠P̯͍̭O̚​N̐Y̡ H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ**](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – adeneo Dec 15 '14 at 21:09
  • 2
    @adeneo - is there a hat for that? – j08691 Dec 15 '14 at 21:10
  • @j08691 - There should be, an easter egg zalgo hat ! – adeneo Dec 15 '14 at 21:11
  • 1
    I'll help you out a little, you're not returning the html anywhere – adeneo Dec 15 '14 at 21:16

2 Answers2

0

Agreed you probably shouldn't do the entire document. Maybe use jQuery('p') or similar.

Use .html( new html that you want ) to write the html.

Something like

$('p').each(function( i, p) { 
       var $p = $(p); 
       $p.html( $p.html().replace(/some regex/, 'replacement')); 
});
Mike Kantor
  • 1,400
  • 4
  • 24
  • 45
0

You can do this, which i think simpler, do what ever you want with the elements

$(function() {
    var html = $('html').html();
    $('p').html(html.replace(/{{text}}/gi, '<i>000000</i>'));
}