Exoplanets are planets that are located outside our solar system - whether that be orbiting a star or drifting past one. Now, the closest star to us is Alpha Centauri which is just over four light-years away. So, how can astronomers detect gases in the atmospheres of planets over this distance with any degree of accuracy (or at all)?
It is only possible to detect gases in transiting Exoplanets. The spectrum of the star is taken when the Exoplanet is not in transit and again when in transit. The differences between the spectrums is due to a small amount of light from the parent star passing through the upper atmosphere of the Exoplanet and being absorbed by gases in the atmosphere. The effect is VERY small and difficult to detect.