Let $I$ be the open interval $(0, 1)$. Let $a, b \in I$ with $a < b$. I would like to approximate the characteristic function $f :=1_{[a, b]}$ in the following sense: we construct a sequence $(f_n) \subset C^\infty_c (I)$ such that
- $f_n \ge 0$.
- $\| f_n - f \|_{L^2 (I)} \to 0$.
- $\| f_n'\|_{L^2 (I)} \to 0$.
We define $g_n$ as follows
- $g_n (x) = 0$ for $x \in (0, a - \frac{1}{n}) \cup (b + \frac{1}{n}, 1)$.
- $g_n (x) = 1$ for $[a, b]$.
- $g_n (x) = n(x-a + \frac{1}{n})$ for $x \in [a - \frac{1}{n}, a)$.
- $g_n (x) = n(-x + b + \frac{1}{n})$ for $x \in [b, b + \frac{1}{n})$.
Then $(g_n) \subset H^1_0 (I)$ satisfies above three conditions. However, $g_n \notin C^\infty (I)$. Could you explain how to get my desired $(f_n)$?

You are wrong in claiming that $\| g_n'\|_{L^2 (I)} \to 0$. Indeed, $\| g_n'\|_{L^2 (I)}^2 = 2n$. On the other hand, such sequence $(f_n)\subset C^\infty_c (I)$ could not exist. This is due to the following remark at page 204 of Brezis' Functional Analysis: