Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript unterminated character class [duplicate]

var badCode = "\(INST1,\\[0,";
var regex = new RegExp(badCode, "igm");

gets "unterminated character class" error.

How to fix?

TIA

trying suggestions from responders to this post, please see the following screen prints (you might have to right-click on images and open in new tab to make legible): note values of new_bad_thing (equiv of badCode above)

and here is the screen print when I hit the run button (please note the error message):

enter image description here

like image 600
davej Avatar asked Feb 01 '13 03:02

davej


2 Answers

Put another backslash at badCode:

var badCode = "\\(INST1,\\[0,";
var regex = new RegExp(badCode, "igm");

since you need one to escape ( inside the regex itself (signal it that it's a literal parentheis) and one escape for javascript.

like image 103
Ofer Zelig Avatar answered Oct 24 '22 03:10

Ofer Zelig


No, it gets "unterminated parenthetical", because you've forgotten to escape the backslash. Why not use a regular expression literal?

var regex = /\(INST1,\[0,/igm;
like image 32
Ry- Avatar answered Oct 24 '22 04:10

Ry-