regex template engine delete carriage return
regex template engine delete carriage return
I'm using this template engine:
https://github.com/krasimir/absurd/blob/master/lib/processors/html/helpers/TemplateEngine.js
from blog:
http://krasimirtsonev.com/blog/article/Javascript-template-engine-in-just-20-line
const templateEngine = (html, options = {}) => {
let re = /<%(.+?)%>/g,
reExp = /(^( )?(var|if|for|else|switch|case|break|{|}|;))(.*)?/g,
code = 'with(obj) { var r=;n',
cursor = 0,
result,
match;
const add = function(line, js) {
js? (code += line.match(reExp) ? line + 'n' : 'r.push(' + line + ');n') :
(code += line != '' ? 'r.push("' + line.replace(/"/g, '\"') + '");n' : '');
return add;
};
while(match = re.exec(html)) {
add(html.slice(cursor, match.index))(match[1], true);
cursor = match.index + match[0].length;
}
add(html.substr(cursor, html.length - cursor));
code = (code + 'return r.join(""); }').replace(/[rtn]/g, ' ');
try {
result = new Function('obj', code).apply(options, [options]);
} catch(err) {
/*
Don't show error when variable is not found, because will be reemplaced with empty string
console.error("'" + err.message + "'", " in nnCode:n", code, "n");
*/
}
return result;
};
but my problem i'm not using it with HTML, but with two simple textarea, a textarea template, and another for the result, the problem is this code, delete the n carriage return
i've tried change this line:
code = (code + 'return r.join(""); }').replace(/[rtn]/g, ' ');
with
code = (code + 'return r.join(""); }');
but i get one simple line; just testing with two lines without variables it's not working.
Example:
line1
my line 2
Result (no break line!):
line1 my line 2
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.