We show that the ring of complex-valued regular functions on an affine irreducible nonsingular real algebraic variety X is factorial if dim X = 1 or dim X = 2 and X has no compact connected components or X is compact and the second cohomology group of X with integral coefficients vanishes.