使用正则表达式中的“行首”匹配符: // X is the single char to add: str = str.replace(/^/gm, "X"); < p > gm 标志会跨越多行进行匹配。 < /p >