Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript error: Invalid regular expression flags

So I have an error in my code that's saying Invalid regular expression flags. The line at fault is this one: ctx.drawImage(/Users/aUser/Desktop/NightSquare.png, 200, 200);, but aUser is replaced with my actual name (changed for privacy). I'm running on a mac and I think I know what the problem is (I didn't include MacIntosh HD), but I'm not sure. How do I fix this?
Additional notes: The /Users/ part of the code is highlighted in red in my text editor (the same color as a string).

like image 674
Hazard Avatar asked Jun 10 '26 01:06

Hazard


1 Answers

Any thing in between / & / is treated as a regular expression in Javascript. There are 2 ways of creating regular expressions in JavaScript.

var myRegEx = new RegExp("pattern") ;
var myRegEx = /pattern/ ;

So using /Users/aUser/Desktop/NightSquare.png is actually leading to your code being interpreted as some regular expression creation which you do not intend. Just make it a string literal( using "" or '') and it will be fine.

In case aUser is a variable ,use the following string concatenation-

"/Users/"+aUser+"/Desktop/NightSquare.png"
like image 121
Rajeev Ranjan Avatar answered Jun 12 '26 16:06

Rajeev Ranjan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!