【发布时间】:2018-05-25 19:04:38
【问题描述】:
我有这个工作函数,检查字符串是否以:结尾
var string = "This is the text:"
function (string) {
if (string.endsWith(':')) {
// ends with :
}
if (string.endsWith(': ')) {
// ends with : and a space
}
else {
// does not end with :
}
}
我还想检查字符串是否以冒号后跟空格,甚至两个空格结尾::_ 或 :__(其中下划线表示此语法中的空格)。
关于如何在不使用多个 if 语句或定义冒号和空格的每个可能组合的情况下实现这一点的任何想法?假设冒号后面可以有任意数量的空格,但如果最后一个可见字符是冒号,我想在我的函数中捕获它。
【问题讨论】:
-
您可能想了解 RegEx。
标签: javascript ends-with