替换/删除两个字符之间的所有内容

时间:2012-05-21 17:30:32

标签: javascript regex

var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

如何删除HS之间的所有内容,以便结果为ABCDEFGHSTUVWXYZ

2 个答案:

答案 0 :(得分:44)

var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
alphabet = alphabet.replace(/H.*S/, 'HS')

或者只是:

var alphabet = "ABCDEFGHSTUVWXYZ";

答案 1 :(得分:9)

var strippedAlphabet = alphabet.replace(/H.*S/, 'HS');