要检查字符串是否以特殊字符结尾

Wanted to check whether string ends with special character or not?

本文关键字:特殊字符 结尾 是否 检查 字符串      更新时间:2023-09-26

我想检查我的字符串是否以特殊字符结束。如果我的字符串末尾包含特殊字符,则需要在右侧进行修剪。如果没有,什么都不做。

我的代码:

var s = 'acbd@';
var x = 'abcd@e'

预期结果:

acbd
abcd@e

有什么帮助吗?

可以使用正则表达式将特殊字符替换为空字符串,如下所示:

s.replace(/[@]+$/, "");
x.replace(/[@]+$/, "");

可以在方括号内指定更多特殊字符