如果语句是针对一个短语而不是特定的标题



我目前正在开发一个cms,它将在多个页面上显示警报。我当前正在使用if语句使警报仅显示在具有特定页面标题的页面上。有没有一种方法可以概括它,并让它出现在所有标题中带有"测试"一词的文章中?

目前我的逻辑是如果@pageTitle === "Test Article Two显示。。。。

我试过做@pageTitle === "Test",但这只显示在标题为Test的文章上,而不是其他标题中包含Test一词的文章上。

这是我的代码:

<script>
if(document.title === "Test Article Two") {
document.body.classList.add("show-alert");
}
</script>

方法-

Regex,区分大小写:

if (/Test/.test(document.title)) { ... }

Regex,不区分大小写

if (/test/i.test(document.title)) { ... }

indexOf,区分大小写,(最快(

if (document.title.indexOf("Test") !== -1) { ... }

包括(ES6(,区分大小写的

if (document.title.includes("Test")) { ... }

您可以使用JavaScript字符串方法includes:if (document.title.includes('Test'))

最新更新