views:

168

answers:

5

I'm trying to match elements with a name that is 'container1$container2$chkChecked', using a regex of '.+\$chkChecked', but I'm not getting the matches I expect when the element name is as described. What am I doing wrong?

+3  A: 

try

string.match( /[$]chkChecked$/ )

alternatively, you could try

string.match( /[^a-zA-Z0-9]chkChecked/ )

( Also, make sure your using // around your regex, otherwise you might be matching using string literals. Not obvious tho without a larger code snippet )

Kent Fredric
A: 

It looks like it should work.

There's a good Javascript Regex Tester that also says it matches.

brien
+3  A: 

my guess, by your use of quotes, is you did something like

re = new RegExp('.+\$chkChecked');

which won't work because js takes advantage of the \ in its string interpretation as an escape so it never makes it into the regex interpreter

instead you want

re = new RegExp('.+\\$chkChecked');
Steven Noble
A: 

Steven Noble:

which won't work because js takes advantage of the \ in its string interpretation as an escape so it never makes it into the regex interpreter

I intended to use \ as an escape because I'm really looking for a $ in the element name.

ProfK
+2  A: 

There's two levels of escaping: one when your code is first parsed (e.g. in case you want to include a ' inside the string), and the second in the regexp engine. So you need two \s before the $ to make the regexp engine not treat it as a special character.

moonshadow