Javascript does not read functions, or does not work if it does not return false

0

It is supposed that my function should calmly replace the content of each element detected by the each, and in turn, also look for the appearance of attachment to later perform a function with each attachment sought, but not what works within while works if I do not put return Cualquiercosa , and if I put return , what follows after while , is omitted with the last function being $(this).html(content) . I do not really know what's happening, or what's going on, it's as if I did not read some lines ...

    $(".x").each(function(){
        content = $(this).text();
        content = content.replace(/\@(.*?)\#([0-9]{5})/g, '<a class="x" x="@$1#$2">$&</a>');
        content = content.replace(/\*{2}(.*?)\*{2}/g, '<text-b>$1</text-b>');
        content = content.replace(/\-{2}(.*?)\-{2}/g, '<text-t>$1</text-t>');
        content = content.replace(/\~{2}(.*?)\~{2}/g, '<text-s>$1</text-s>');
        attachment = /\{{2}attachment:(.*?)\}{2}/g;
        attachmentsCount = (content.match(attachment) || []).length;
        while((image = attachment.exec(content))) {
        $("<img>").attr("src", "url=" + image[1]).appendTo($(this).children("x"));
        }
        var content = content.replace(attachment, '')
        $(this).html(content);
    })
    
asked by Esteban Fernández 06.08.2018 в 00:11
source

1 answer

1

The condition of the while to the always seems to be true and stays in an infinite loop and what it seems is that you modify content outside the cycle and not inside.

This code should work

 $(".x").each(function(){
    content = $(this).text();
    content = content.replace(/\@(.*?)\#([0-9]{5})/g, '<a class="x" x="@$1#$2">$&</a>');
    content = content.replace(/\*{2}(.*?)\*{2}/g, '<text-b>$1</text-b>');
    content = content.replace(/\-{2}(.*?)\-{2}/g, '<text-t>$1</text-t>');
    content = content.replace(/\~{2}(.*?)\~{2}/g, '<text-s>$1</text-s>');
    attachment = /\{{2}attachment:(.*?)\}{2}/g;
    attachmentsCount = (content.match(attachment) || []).length;
    while((image = attachment.exec(content))) {
    $("<img>").attr("src", "url=" + image[1]).appendTo($(this).children("x"));
    content=content.replace(attachment, '');
    }

    $(this).html(content);
})
    
answered by 06.08.2018 / 03:00
source