views:

33

answers:

2

I have been trying to figure out regex myself and to no avail can I get the last bracket to disappear from a string.

For example:

[The Day the Earth Stood Still]

I can only get:

Day the Earth Stood Still]

with the following RegEx code:

/(\[|\](^The ))\2/

I'm aiming for just:

Day the Earth Stood Still

Any help would be greatly appreciated. I've spent 3 hours trying to figure it out on my own... This is me giving in. :3

+1  A: 

You can try:

\[The\s(.*)]

If you need this to work to strip out brackets even when 'The' is not present you can try:

(?:\[The\s|\[)(.*)]

If you think you will run into a case where you may have 'the' or 'The' you can try:

(?:\[[Tt]he\s|\[)(.*)]

Here is some code to implement capturing text without the brackets and 'The':

var title = new Array();
title[0] = "[The Day the Earth Stood Still]";
title[1] = "[Independence Day]";
title[2] = "[the Day the Earth Stood Still]";

alert(title[0].match(/(?:\[[Tt]he\s|\[)(.*)]/)[1]);
alert(title[1].match(/(?:\[[Tt]he\s|\[)(.*)]/)[1]);
alert(title[2].match(/(?:\[[Tt]he\s|\[)(.*)]/)[1]);

Try it out here: http://jsfiddle.net/aSwYz/

tinifni
Well it erases the brackets and "The," but it deletes everything else too.
Mattan138
What is the code you're using?
tinifni
I tried all of them and ended up with the same result.
Mattan138
Works perfectly in win/FF3.6
Moses
Are you saying that the jsfilddle link does not provide the desired results for you? Can you edit your question to provide the code you are using that is not providing the desired results?
tinifni
Your example works perfectly. There has to be something else wrong with my code. I'll try some stuff.
Mattan138
A: 

Maybe you should take the approach of thinking of what you want to keep instead of what you want to get rid of

(?<=\[The\s*)[\w\s]*(?=\])

Otherwise, you are trying to do a match in separate parts of the string and should be done with 2 different matches

Seattle Leonard