Hessenberg varieties are subvarieties of the flag variety parametrized by a linear operator $X$ and a nondecreasing function $h$. The family of Hessenberg varieties for regular $X$ is particularly important: they are used in quantum cohomology, in combinatorial and geometric representation theory, in Schubert calculus and affine Schubert calculus. We show that the classes of a regular Hessenberg variety in the cohomology and $K$-theory of the flag variety are given by making certain substitutions in the Schubert polynomial (respectively Grothendieck polynomial) for a permutation that depends only on $h$. Our formula and our methods are different from a recent result of Abe, Fujita, and Zeng that gives the class of a regular Hessenberg variety with more restrictions on $h$ than here.