I have a question regarding creating a regex search on a string. The code is in javascript, I am having problem with searching using this regex
var string = 'string [something]';
var regex = new RegExp(string,'g');
Whenever i try to use search() method on a bigger string i.e (searching for the above string on a bigger string) i get an Syntax error SyntaxError: Invalid regular expression: /string [something]/: Range out of order in character class
The regex string is not valid because '[' and ']' are reserved expressions. You need to escape them
var string = 'string \\[something\\]';