My attempt was :
var re = new RegExp("\w{" + n + "}", "g");
But it didn't seems to work.
P.S. - I have searched several questions of Stackoverflow thinking it must have been asked before But I didn't find one, so I asked my question.
The problem is that \ is not only the escape character in regex but also in JS strings. So when you create a regular expression from a string you need to escape it. This means that \w becomes "\\w" in a string and if you want to match a single \ it would even become "\\\\".
Instead of changing it to \\w you can also use . if you don't care about the characters or if the string was validated before.
\ is not only the escape character in regex but also in JS strings I didn't knew that earlier, any link where where I can learn about that is much appreciated ^_^
var arr = str.match(re);where str is the variable having the string.