My title might not be worded correctly sorry in advance. I've looked everywhere and I honestly can't seem to figure it out. I want to use a variable in a RegEx but the solution that i've found and tried to work off of works but it is not flexible for what I need. I can't seem to figure out how to convert it to a RegEx constructor object which I think is my best bet. Any help will be greatly appreciated.
let str = 'haeandviaecy'
let newString = str.replace(/(\w{3})/g, '$1 ').replace(/(^\s+|\s+$)/, '')
//hae and via ecy
replace(/(^\s+|\s+$)/,'') removes any preceding or trailing space from the string - just giving you a heads up
So what this snippet does is go to every third and put a blank space in it. What I want is to make my code more flexible and be able to put a variable to let me choose the number for when it puts a blank space using a variable.
right now it prints out - "hae and via ecy" for ex. every 4th would be - haea ndvi aecy
I've read you cant put in variables unless it is a RegEx contructor object. When I've tried to convert it doesn't seem to work.