You need to double escape any RegExp characters (once for the slash in the string and once for the regexp):(你需要双重转义任何RegExp字符(一次用于字符串中的斜杠,一次用于regexp):)
"$TESTONE $TESTONE".replace( new RegExp("\$TESTONE","gm"),"foo")
Otherwise, it looks for the end of the line and 'TESTONE' (which it never finds).(否则,它会查找该行的结尾和'TESTONE'(它从未找到)。)
Personally, I'm not a big fan of building regexp's using strings for this reason.(就个人而言,由于这个原因,我不是建立regexp使用字符串的忠实粉丝。) The level of escaping that's needed could lead you to drink.(逃脱的水平可能导致你喝酒。) I'm sure others feel differently though and like drinking when writing regexes.(我确信其他人会有不同的感受,并且在写正则表达时喜欢喝酒。) 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…