Question: So I really have no clue why this isn't wokring - basic stuff I did hundred times before:

I want to disable the button 'loadGame' by doing the following if the boolean is false:

```
if(saveExists == false){
        loadGame.setEnabled(false);
    }else{
        loadGame.setEnabled(true);
    }

```

saveExists is a boolean taken from a sharedPreferences object which is by default false (works, because I already checked saveExists in an output line).
Basically no matter what cases I create with if/elseif/else the button doesn't seem to get disabled at any point - did I really miss something big? 

---

Okay so here's some code concerning the SharedPreference:

```
SharedPreferences settings;
SharedPreferences.Editor preferenceEditor;

```

...

In the onCreate of the class where I want to disable the button:

```
Resource.settings = getSharedPreferences(Resource.PREFERENCE_AUDIO, MODE_PRIVATE);
    Resource.preferenceEditor = Resource.settings.edit();
    saveExists = Resource.settings.getBoolean("settings", false);
    Log.d(TAG, "saveExists="+saveExists);

```

and finally, the part to disable the Button:

```
loadGame = (ImageButton) findViewById(R.id.loadButton);
    loadGame.setEnabled(saveExists);
    loadGame.setOnClickListener(new OnClickListener() {...}

```

Oh btw we're talking about a ImageButton here

**SOLUTION: You won't see this coming** 

God I hate to break it to you but that was clearly my fault. My projectpartner toggled the buttons invisible/visible as part of the AsyncTask and it's executed after I'm doing what I tried here all the time - yup shame on me I guess :x

Answer: 
Assuming that the brackets are always correctly balanced (even within strings or comments) and can never be nested or escaped, you can solve this in a single regex using [lookahead assertions](http://www.regular-expressions.info/lookaround.html):

```
result = subject.replace(/([:.])(?=[^[\]]*\])/g, "\\$1");

```

As a commented regex:

```
([:.])    # Match and remember a dot/colon,
(?=       # only if it is followed by:
 [^[\]]*  # any number of characters excluding brackets,
 \]       # followed by a closing bracket.
)         # End of lookahead assertion.

```

The lookahead ensures that the next bracket after the dot/colon we're looking at is a closing bracket.
I only know how to do it with a replace function:

```
var test = "[ab:1.2:ef]='12.3'";

var out = test.replace(/\[.*?\]/g, function(str) {
    return(str.replace(/\.|:/g, "\\$&"));
});

// out is [ab\:1\.2\:ef]='12.3'

```

See it in action here: <http://jsfiddle.net/jfriend00/ZHVjW/>