Use this:
var result:String = string.replace(/([^a]) | ([^b])/g, "$1_$2");
A simplified explanation of the above is that it replaces a space that either:
- preceded by a character other than
a - followed by a character other than
b
Note. To generalize the regex to include tabs and newlines, use \s , for example:
var result:String = string.replace(/([^a])\s|\s([^b])/g, "$1_$2");
source share