检查字符串是否以多个点regex JS结尾

fwzugrvs  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(79)

我使用这个模式/\.\.+$/g来检查字符串是否以多个点结束。然而,它没有工作。我不知道我的代码哪里出错了,请帮助!

<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>

<script>
let text = "...Is this .. all there is..?";
//check if string end with more than one dot
let pat = /\.\.+$/g
let result = pat.test(text)
//should return true
document.getElementById("demo").innerHTML = result;
</script>
</body>
</html>
3gtaxfhh

3gtaxfhh1#

使用\.{2,}检测2个或更多点。另外,如果您打算使用问号或感叹号,则应添加[?!]?

let text = "...Is this .. all there is..?";

console.log(/\.{2,}[?!]?$/.test(text));

如果您想同时检测点和标记:

let text = "...Is this .. all there is..?";

console.log(/[.?!]{2,}$/.test(text));

相关问题