获取通用跨度数据的特定数据

时间:2018-05-02 23:42:21

标签: javascript jquery

我有像这样的人:

<span data-sp-peoplepickerprocesseduser="true" id="Notificar_x0020_a_084ffd45-b361-458e-b55f-c824ba8995ec_$ClientPeoplePicker_i:0#.f|membership|email@mydomain.com_ProcessedUser0" resolveduser="true" sid="i:0#.f|membership|email@mydomain.com" class="sp-peoplepicker-userSpan">

我想知道如何检索每个范围的检索值email@mydomain.com(我可以有多个),并将其区分为不会重复结果。此致

2 个答案:

答案 0 :(得分:1)

这适用于您当前的示例。

$(function () {
   // get value from sid attribute
   var sid = $('span').attr('sid');
   // extract email using regex
   var matches = sid.match(/([^|]*@[^|]*)/);
   // get email
   var email = matches[0];
   
   console.log(email);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span data-sp-peoplepickerprocesseduser="true" id="Notificar_x0020_a_084ffd45-b361-458e-b55f-c824ba8995ec_$ClientPeoplePicker_i:0#.f|membership|email@mydomain.com_ProcessedUser0" resolveduser="true" sid="i:0#.f|membership|email@mydomain.com" class="sp-peoplepicker-userSpan"></span>

欲了解更多信息:

答案 1 :(得分:1)

&#13;
&#13;
var result=[]; 
 //find all spans
$('span').each(function() {
   //find all attributes
  $.each(this.attributes, function() {
  	    var this_val=this.value;
  	    var this_val_arr=this_val.split('|');
  	    $.each(this_val_arr, function() {
  	    	 var this_val_arr_more=this.split('_');
  	    	 $.each(this_val_arr_more, function() {
  	    	      if(this.indexOf('@') != -1){  //found @ in string
                      var found_str=this.toString();
                      if(result.indexOf(found_str) == -1){  //not found in result
                          result.push(found_str);
                      }
  	    	      }
  	    	 });
  	    });
  });
});
console.log(result);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>



<span data-sp-peoplepickerprocesseduser="true" id="Notificar_x0020_a_084ffd45-b361-458e-b55f-c824ba8995ec_$ClientPeoplePicker_i:0#.f|membership|email@mydomain.com_ProcessedUser0" resolveduser="true" sid="i:0#.f|membership|email@mydomain.com" class="sp-peoplepicker-userSpan"></span>
&#13;
&#13;
&#13;