For the past several decades, we have been able to directly probe the motion of atoms that is associated with chemical transformations and which occurs on the femtosecond (10−15-s) timescale. However, studying the inner workings of atoms and molecules on the electronic timescale1, 2, 3, 4 has become possible only with the recent development of isolated attosecond (10−18-s) laser pulses5. Such pulses have been used to investigate atomic photoexcitation and photoionization6, 7 and electron dynamics in solids8, and in molecules could help explore the prompt charge redistribution and localization that accompany photoexcitation processes. In recent work, the dissociative ionization of H2 and D2 was monitored on femtosecond timescales9 and controlled using few-cycle near-infrared laser pulses10. Here we report a molecular attosecond pump–probe experiment based on that work: H2 and D2 are dissociatively ionized by a sequence comprising an isolated attosecond ultraviolet pulse and an intense few-cycle infrared pulse, and a localization of the electronic charge distribution within the molecule is measured that depends—with attosecond time resolution—on the delay between the pump and probe pulses. The localization occurs by means of two mechanisms, where the infrared laser influences the photoionization or the dissociation of the molecular ion. In the first case, charge localization arises from quantum mechanical interference involving autoionizing states and the laser-altered wavefunction of the departing electron. In the second case, charge localization arises owing to laser-driven population transfer between different electronic states of the molecular ion. These results establish attosecond pump–probe strategies as a powerful tool for investigating the complex molecular dynamics that result from the coupling between electronic and nuclear motions beyond the usual Born–Oppenheimer approximation.