2

Short: I'm looking for something like jQuery's .each() function with regex.

I've got a long text with some short strings in the format of <abcd1234>. The abcd1234 should be wrapped in a <span> including text color which is generated for every found string. I found this function to do so: https://stackoverflow.com/a/3426956/237312

function nickFormat(text) {
            var exp = /\&lt\;(.*)\&gt\;/ig;
            name = exp.exec(text);
            return text.replace(exp, "&lt;<span style='color: #"+intToARGB(hashCode(RegExp.$1)).substr(0, 6)+"'>$1</span>&gt;");
        }

This is my current code to replace the found string/s, which means the regex is working. But not as initially intended. Every found string is colored with the same color.

Any idea, how to solve it?

2

1 Answer 1

2

Thanks to Felix Kling I was able to fix the problem really fast. Thanks!

function nickFormat(text) {
    var exp = /\&lt\;(.*)\&gt\;/ig;

    function makeItSo(match) {
        return "<span style='color: #"+intToARGB(hashCode(match)).substr(0, 6)+"'>"+match+"</span>";
    }
    return text.replace(exp, makeItSo);
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.