Didn’t want to ask, but I gave up :-( I can’t find the solution
I can’t get how to escape the variable/string.
var highlight_chars_count = {{highlight_chars_count}};
var regex_string = "/(^\\w{" + highlight_chars_count + "})/";
var regex = new RegExp(regex_string);
$('h1').html(function(i, v) {
return v.replace(regex, "<span>$1</span>");
});
This pattern works (without " and a variable)
var regex = new RegExp(/(^\w{2})/);
I think the solution is here JavaScript regex pattern concatenate with variable … but can’t transfer that to my regex.
The variable {{highlight_chars_count}} is a twig variable. Unfortunately I can’t insert the variable into the regex pattern either.