BorisMoore / jsrender

A lightweight, powerful and highly extensible templating engine. In the browser or on Node.js, with or without jQuery.
http://www.jsviews.com
MIT License
2.67k stars 339 forks source link

regex not working #359

Closed romanown closed 4 years ago

romanown commented 4 years ago

{{:key.replace(/[^a-zA-Z0-9]/, "_")}} if argument 38.5 result 8.5. if `{{:key.replace(/[^a-zA-Z0-9]/g, "")}}error Object { name: "JsRender Error", message: "Syntax error\nCompiled template code:\n\n// jsvTmpl/if/for\nvar v,t=j._tag,ret=\"\"+\"\n\t\t\t\t\t\t<div class=\\"js-block\"\n+((v=data.key.replace(/[j.sq(.a-data.zA-data.Z0-9)]/data.g,\"\"))!=null?v:\"\")`

BorisMoore commented 4 years ago

JsRender tags only allow 'valid JsRender paths and expressions' (see https://www.jsviews.com/#paths) and that does not include global functions such as RegExp() nor does it include RegExp expressions /.../.

You can either preprocess your data values before passing them to JsRender, or if you want you can use a helper function or converter, so your RegExp code is not directly in the template markup. You can even pass in the regular expression as a parameter to the helper function or converter, as in this example:

$.views.converters("regexReplace", function(value, regex, repl) {
    return value.replace(new RegExp(regex, 'g'), repl)
});

with

{{regexReplace:key "[^a-zA-Z0-9]" "_"}}
romanown commented 4 years ago

thank You for fast answere