jQuery - 按符号分割字符串并获取最后一部分

时间:2016-06-08 10:55:29

标签: jquery arrays string

如果我有一个银行帐号列表,如下所示:

<span>6456465465/0471</span>
<span>4547/6456465465/0471</span>
<span>4547/6465465/0471</span>

如何从这些中获取最后四位数字?有些银行账户有一个前缀,有些则没有。

我的代码仅适用于第一个范围,因为其余帐户都有前缀。

$("span").each(function(){
  var kodBanky = $(this).text().split("/")[1];      
  $('body').append("<p>" + kodBanky + "</p>");    
});

Codepen

6 个答案:

答案 0 :(得分:2)

使用 Array#pop() 方法从split数组中获取最后一个元素

$("span").each(function(){
  var kodBanky = $(this).text().split("/").pop();
  $('body').append("<p>" + kodBanky + "</p>");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span>6456465465/0471</span>
<span>4547/6456465465/0471</span>
<span>4547/6465465/0471</span>

虽然您可以使用 String#match() 方法或 String#slice() 方法

$("span").each(function(){
  var kodBanky = $(this).text().match(/\d{4}$/)[0];
  // or
  // var kodBanky = $(this).text().slice(-4);
  $('body').append("<p>" + kodBanky + "</p>");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span>6456465465/0471</span>
<span>4547/6456465465/0471</span>
<span>4547/6465465/0471</span>

答案 1 :(得分:1)

尝试使用从(字符串大小 - 4)到(字符串大小)

的子字符串
var digits = yourSpanVar.substring(yourSpanVar.length-4, yourSpanVar.length);

答案 2 :(得分:1)

有一些方法可以做到这一点,基础是找到最后一部分,然后使用最后一部分:

// iterate over each <span>:
$("span").each(function() {
  // find the text of the current <span>, split
  // on the '/' characters, and retrieve that last
  // part:
  var kodBanky = $(this).text().split("/").pop();

  // append a newly created <p> element to the
  // <body> element:
  $('<p/>', {
    'text' : kodBanky
  }.appendTo('body');
});

&#13;
&#13;
$("span").each(function() {
  var kodBanky = $(this).text().split("/").pop();
  $('body').append("<p>" + kodBanky + "</p>");
});
&#13;
span {
  display: block;
  margin: 0 0 0.5em 0;
  border-bottom: 1px solid #ccc;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span>6456465465/0471</span>
<span>4547/6456465465/0471</span>
<span>4547/6465465/0471</span>
&#13;
&#13;
&#13;

值得注意的是,jQuery几乎肯定不需要像这样简单的东西:

// get all the <span> elements in the document, and convert
// the returned collection, from document.querySelectorAll(),
// into an Array, using Array.from():
var spans = Array.from(document.querySelectorAll('span')),

  // create a <p> element:
  para = document.createElement('p'),

  // two variables for later use:
  clone,
  code;

// iterating over the Array of <span> elements:
spans.forEach(function(span) {
  // 'span' refers to the current array-element,
  // a <span> element, within the array of <span>
  // elements over which we're iterating.

  // code is the text contained within the current
  // <span> after it's trimmed of its leading, and
  // trailing, white-space. We then split that text
  // on the '/' characters, and retrieve the last
  // Array element of the Array using
  // Array.prototype.pop()
  code = span.textContent.trim().split('/').pop();

  // we clone the paragraph:
  clone = para.cloneNode();

  // updating its textContent:
  clone.textContent = code;

  // and append that created-<p> element to
  // document.body:
  document.body.appendChild(clone);
});

&#13;
&#13;
var spans = Array.from(document.querySelectorAll('span')),
  para = document.createElement('p'),
  clone,
  code;

spans.forEach(function(span) {
  code = span.textContent.trim().split('/').pop();
  clone = para.cloneNode();

  clone.textContent = code;
  document.body.appendChild(clone);
});
&#13;
span {
  display: block;
  margin: 0 0 0.5em 0;
  border-bottom: 1px solid #ccc;
}
&#13;
<span>6456465465/0471</span>
<span>4547/6456465465/0471</span>
<span>4547/6465465/0471</span>
&#13;
&#13;
&#13;

参考文献:

答案 3 :(得分:0)

这可能会有所帮助。这将给出最后4位数字。

$("span").each(function(){
  var kodBanky = $(this).text();
      kodBanky = kodBanky.slice(-4);      
  $('body').append("<p>" + kodBanky + "</p>");    
});

答案 4 :(得分:0)

您可以使用String.lastIndexOf

$(function () {
 
  $("span").each(function(index, ele){
    var kodBanky = ele.textContent.substr(ele.textContent.lastIndexOf('/') + 1);
    $('body').append("<p>Result " + index + ' --> ' + kodBanky + "</p>");
  });
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<span>6456465465/0471</span><br>
<span>4547/6456465465/0471</span><br>
<span>4547/6465465/0471</span><br>

另一种可能的方法是:

$(function () {
  
  $("span").each(function(index, ele) {
    // look for the last match..
    var kodBanky = ele.textContent.split(/^.*\/([^/]+)$/).join('');
    $('body').append("<p>Result " + index + ' --> ' + kodBanky + "</p>");
  });
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<span>6456465465/0471</span><br>
<span>4547/6456465465/0471</span><br>
<span>4547/6465465/0471</span><br>

答案 5 :(得分:0)

这应该很容易做到

var kodBanky = "";
$("span").each(function(){
  kodBanky = $(this).text();
  kodBanky =  kodBanky.substr(kodBanky.length - 4); 
  alert(kodBanky);
});

Demo此处

相关问题