Использовать RegExp. Не забывайте избегать специальных символов.
s1 = s1.replace(/~+$/, ''); //$ marks the end of a string // ~+$ means: all ~ characters at the end of a string