Reputation: 4690
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 = /\<\;(.*)\>\;/ig;
name = exp.exec(text);
return text.replace(exp, "<<span style='color: #"+intToARGB(hashCode(RegExp.$1)).substr(0, 6)+"'>$1</span>>");
}
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?
Upvotes: 2
Views: 110
Reputation: 4690
Thanks to Felix Kling I was able to fix the problem really fast. Thanks!
function nickFormat(text) {
var exp = /\<\;(.*)\>\;/ig;
function makeItSo(match) {
return "<span style='color: #"+intToARGB(hashCode(match)).substr(0, 6)+"'>"+match+"</span>";
}
return text.replace(exp, makeItSo);
}
Upvotes: 2