views:

55

answers:

3

I am trying to extract the word "need" from this string.

ctl00_ctl00_ContentMainContainer_ContentColumn1__needDont_Panel1

I have tried [__]([.]?=Dont)

This is using javascript .match()

I have even tried to use http://gskinner.com/RegExr/ but just can't solve this one. Thanks for the help!

+1  A: 

This will accomplish what you're looking for:

__(.*)(?=Dont)

You seem to be mixing up what a character class - square brackets [] - does, instead you should be using regular brackets ().

In your regex [__] will only match a single underscore _ and [.] will match a single period.

Gavin Miller
Thanks for the info. Unfortunately your regex returned an array of two values "__need" and "need"
a432511
1+ for helping!
a432511
Javascript doesnt supports lookbehinds i guess, so this is the best method here. And it works! I just have to access result[1] of that array. Thanks again
a432511
+1  A: 

Your error is writing [__] instead of __ (without the braces). [__] matches only a single underscore, so it will match _ctl00_ContentMainContainer_ContentColumn1__need.

[.] is also wrong. You should use something like: [^_]+ (anything except underscore).

Mark Byers
+2  A: 
(?<=__)\w+(?=Dont)

Matches all alpha-numbers between __ and Dont

Edit

Sorry, I havent noticed word JavaScript. It does not support lookbehind, so __(\w+)(?=Dont) can be used there. If Regex should match even when nothing comes between __ and Dont use "\w*" instead of "\w+". Be careful with ".*" because dot matches almost all characters, do you allow spaces in ID?

I haven't noticed

Roman
1+ for helping!
a432511
No... there wont be a case where there are spaces. Your answer is also a very good answer because this regex produces the result I want, just not in javascript. Thanks again!
a432511