jQuery:如果内容与正则表达式模式匹配,则执行此功能

jQuery: Do function if content matches regex pattern

本文关键字:执行 功能 模式匹配 正则表达式 如果 jQuery      更新时间:2023-09-26

我想对内部内容与正则表达式/^'s*o's+/m匹配的每个<p>元素运行jQuery函数。我不知道该怎么做。建议,请。。。

使用jQuery filter():

$('p').filter(function() {
    return (/^'s*o's+/).test($(this).text()); // return every match as jQuery obj
}).css('background-color', 'red'); // <--example, do your stuff here.
  • 演示
  • .filter()jQuery
$("p").each(function () {
   if (/^'s*o's+/m.test(this.innerHTML)) {
        // what to do here.
   }
})

这就是你的做法,但你应该处理正则表达式,因为我真的不知道它是做什么的。