Это будет делать трюк:
var input = '#ff0000He#ffccccllo'.split('#');
var output = '';
input.filter(function (str) {
return !!str;
}).forEach(function (str) {
output += '<span style="#' + str.slice(0, 6) + '">' + str.slice(6) + '</span>';
})
console.log(output) // --> <span style="...
Или как функцию:
function convert(input) {
input = input.split('#');
var output = '';
input.filter(function (str) {
return !!str;
}).forEach(function (str) {
output += '<span style="#' + str.slice(0, 6) + '">' + str.slice(6) + '</span>';
})
return output;
}
ОБНОВЛЕНИЕ: исправлена ошибка в функции dfn. Работает сейчас.