I want to change the value of a variable in js file using php, what i tried so far is to get the file contents and try to find a robust regex formula to change the value of the variable:
here is the contents of the js file, keeping in mind that the value can be anything between = and ;
// blah blah blah
const filename = ""; // it can be filename = ''; or filename = null; or filename = undefined; and so on
// blah blah blah
i tried to get that exact line using this: preg_match
/(((\bconst\b)+\s*(\bfilename\b)+\s*)+\s*=)[^\n]*/is
then replaced the value usning this: preg_replace
/([\"\'])(?:(?=(\\?))\2.)*?\1/is // to get what is between ""
or
/(?<=\=)(.*?)(?=\;)/is // to get what is between = & ;
then replaced the whole line again in the file usning the first formula: preg_replace
/(((\bconst\b)+\s*(\bfilename\b)+\s*)+\s*=)[^\n]*/is
I'm asking is their a better approach, cause i feel this is too much work and not sure about the elegance and performance of what i did so far!
NOTE: its a rollup config file and will get the bundle filename from the controller/method of current php method >> its a specific scenario.