1

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?

1
  • So what are you expecting and what are you getting? Commented Sep 18, 2008 at 11:06

5 Answers 5

3

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');
Sign up to request clarification or add additional context in comments.

Comments

3

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 )

Comments

2

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.

Comments

0

It looks like it should work.

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.