Javascript 从Div获取匹配的文本

Javascript 从Div获取匹配的文本,javascript,jquery,Javascript,Jquery,我有以下div,其中包含我需要与逗号分隔值匹配的某些文本: <div class="val"> This is para 1 </div> <div class="val"> This is para 2 </div> 因此,对于一个字符串(逗号分隔的字符串除外),我可以匹配单个字符串,并根据该文本进行如下更改: $( ".val:contains('This is para 1

我有以下div,其中包含我需要与逗号分隔值匹配的某些文本:

<div class="val">
    This is para 1
</div>

<div class="val">
    This is para 2
</div>
因此,对于一个字符串(逗号分隔的字符串除外),我可以匹配单个字符串,并根据该文本进行如下更改:

$( ".val:contains('This is para 1')" ).css( "color", "green" );
但我不知道如何才能做到这一点

$( ".val:contains('This is para 1, This is para 2')" ).css( "color", "green" );

这意味着它应该将颜色更改为相应的div。因此我认为,我需要对文本进行某种拆分,以相应地匹配它们,但有点混乱。任何更好的方法或想法都将不胜感激-谢谢。

我将重构服务器发送的内容,也许是一个有效的JSON?。尽管如此,要以这两个.val为目标,您需要分别选择它们。下面的代码段假设服务器将发送这是第1段,这是第2段,并为jQuery构建选择器

const res=`这是第1段,这是第2段'; const selector=res.split','.maps=>`.val:包含'${s.trim}'`.join','; 控制台。日志选择器; $selector.css颜色,绿色; 这是第1段 这是第2段
另一种选择如下:

// get, and cache, the server response:
let needles = 'Paragraph 2, Paragraph 8, Paragraph 1',

// split that string on the comma character, using
// String.prototype.split(); and then iterate over
// the resulting Array using Array.prototype.map()
// to trim the Array of strings of their leading or
// trailing white-space using String.prototype.trim():
    needlesArray = needles.split(',').map((n) => n.trim());

// here we iterate over the needlesArray, using
// Array.prototype.forEach():
needlesArray.forEach(

  // this is an Arrow function expression in which the 'str'
  // (the first argument) is a reference to the current
  // Array-element value; we concatenate that into the
  // selector String to retrieve the relevant elements, and
  // then use jQuery's addClass() method to add the 'matched'
  // class-name to the element(s):
  (str) => $('div:contains(' + str + ')').addClass('matched')
);
设针='第2段、第8段、第1段', needersarray=针.split','.mapn=>n.trim; 针刺 str=>$'div:包含'+str+.addClass'matched' ; *, ::之前, ::之后{ 框大小:边框框; 保证金:0; 填充:0; } .匹配{ 背景:向右线性梯度,灰白,fff6; } 第1款 第2款 第3款 第4段 第5段 第6段 第7段 第8段 第9段 段落10您可以将jQuery.filter方法与array.includes方法一起使用:

设str='这是第1段,这是第2段,这是第c段'; $.val.filterfunction{ 返回str.split/,|,/。包括this.textContent.trim; } .css颜色,绿色; 这是第1段 这是a段 这是b段 这是第2段 这是c段 这是d段
您是否已采取预防措施,防止CSV中包含逗号?他们会逃脱还是永远不会在那里?他们会在比赛中逃脱@David说让Monica复职。但默认情况下,它们会留在那里。@F.Müller,如果你仔细看,我用的是s.trim。也许模板文字中的$让您感到困惑;的确应该仔细看看。那就更好了。我想你还是可以用。trimStart或。trimsleft。
// get, and cache, the server response:
let needles = 'Paragraph 2, Paragraph 8, Paragraph 1',

// split that string on the comma character, using
// String.prototype.split(); and then iterate over
// the resulting Array using Array.prototype.map()
// to trim the Array of strings of their leading or
// trailing white-space using String.prototype.trim():
    needlesArray = needles.split(',').map((n) => n.trim());

// here we iterate over the needlesArray, using
// Array.prototype.forEach():
needlesArray.forEach(

  // this is an Arrow function expression in which the 'str'
  // (the first argument) is a reference to the current
  // Array-element value; we concatenate that into the
  // selector String to retrieve the relevant elements, and
  // then use jQuery's addClass() method to add the 'matched'
  // class-name to the element(s):
  (str) => $('div:contains(' + str + ')').addClass('matched')
);
    let str = 'This is para 1, This is para 2,This is para c';
    $(".val").filter(function() { 
        return str.split(/, |,/).includes( this.textContent.trim() ); 
    })
    .css( "color", "green" );