使用 jQuery 获取字符串的一部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3722360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:59:00  来源:igfitidea点击:

Get part of a string using jQuery

jquerystringsubstring

提问by VladY

HTML code:

HTML代码:

<div id="block-id-45"></div>

How can I get number "45"of string using jQuery?

如何使用jQuery获取字符串的数字“45”

回答by Peter Ajtai

To return the number at the end of an idattributeuse

为了在年底返回数字id属性使用

$(this).attr("id").match(/[\d]+$/);

The above will return 45if $(this)is <div id="block-id-45"></div>

45如果$(this)是,上面将返回<div id="block-id-45"></div>

jsFiddle example

jsFiddle 示例

The way the above works is that you retrieve the id of the element using .attr(), then you look at the idand use .match()to recover the number at the end of it. /[\d]+$/is a regex. [\d]means one digit +means one or more (of the digits). and $means the end of the line.

上面的工作方式是使用 检索元素的 id .attr(),然后查看id并使用.match()来恢复它末尾的数字。/[\d]+$/是一个正则表达式。[\d]表示一位数+表示一位或多位(数字中的)。并$表示该行的结尾。



You can use this function to retrieve the numbers from the end of all divs with an id that starts with block-id-by making use of the attribute starts with selector [name^=value]and .each():

您可以使用此函数block-id-通过使用属性以选择器[name^=value]开头来检索所有 div 末尾的数字,该 id 以 id 开头.each()

Practical usage:

实际用法:

$(function() {

      // Select all DIS that start with 'block-id-'
      //   and iterate over each of them.
    $("div[id^='block-id-']").each(function() {

          // You could push this data to an array instead.
          // This will display it.
        $("body").append( "Id number: " + 
                            // This is the number at the end
                          $(this).attr("id").match(/[\d]+$/) +
                          "<br/>" );
    });
});?

jsFiddle example

jsFiddle 示例

回答by T.J. Crowder

You don't need (or particularly want) jQuery for this (it's very useful for lots of otherthings, just not particularly for this). Straight JavaScript and DOM:

你不需要(或特别想要)jQuery(它对很多其他事情非常有用,只是不是特别适合这个)。直接的 JavaScript 和 DOM:

var div = document.getElementById('block-id-45');
var n = div.id.lastIndexOf('-');
var target = div.id.substring(n + 1);

Live example: http://jsbin.com/osozu

实例:http: //jsbin.com/osozu

If you're already using jQuery, you can replace the first line with:

如果您已经在使用 jQuery,则可以将第一行替换为:

var div = $('#block-id-45')[0];

...but there's little if any reason to.

...但几乎没有理由这样做。

回答by Erdin? ?orbac?

Using jQuery, simply:

使用jQuery,只需:

$("[id='block-id-45']").attr("id").split("-")[2]

For all block-id-##, you can use mask pattern from Peter's answer:

对于所有 block-id-##,您可以使用Peter 回答中的掩码模式:

$("[id^='block-id-']").click(function(){

    row_id = $(this).attr("id").split("-")[2];
    .............
    .............
})