A
A
Alex2016-08-26 14:50:49
JavaScript
Alex, 2016-08-26 14:50:49

What is the purpose of the outer loop in this code snippet?

Here is a cycle taken from the Vkontakte function for parsing emoticons

for (var i = 0; i < 2; i++) {
    for (var code in regs) {
      str = str.replace(regs[code], function(match, pre, smile, space) {
        return (pre || '') + Emoji.getEmojiHTML(code)+(space || '');
      });
    }
  }

Link to the file - https://vk.com/js/al/emoji.js?233246401
Line - 1843.
As far as I understand, the inner loop goes through the object with regular expressions and replaces all occurrences with the corresponding emoticon code. But why do it twice? What is the outer loop for?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Osher, 2016-08-26
@miraage

I will assume that all emoji are represented by two bytes.

A
Alexey, 2016-08-26
@alsopub

I don’t know how VK does it, but in such tasks it happens that after the first pass, as a result of substitutions, new elements are formed that can be replaced, but the first cycle has already been completed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question