For the solution, you can use the replacement by a regular expression. For this, the lines have a .replace method
The regular expression can be the following: #(\w+) - a substring that starts with # is selected and includes characters from the set: A-Za-z0-9_ .
The second parameter, .replace can take the substitution function, the parameters of which are the found substrings and the specified groups.
The function might look like this:
function(_, $1) { // первый параметр не важен, во втором приходит содержимое группы, а именно "class" или "text" из примера, без # return obj[$1] || $1; // если есть поле в объекте возвращаем значение, иначе оставляем как есть }
Example assembly:
var html = '<span class = "#class">#text</span>'; var obj = { class: 'class1', text: 'Span Text' }; console.log(html.replace(/#(\w+)/g, function(_, $1) { return obj[$1] || $1; }))