javascript - 如何使用 jquery 或 javascript 从字符串中查找完全不同的数字

标签 javascript jquery html blogger

我需要从字符串值中查找数字,例如:

tag:example.com,1999:blog-124938250235712450.post-092348192357990021

我通过 JQuery 使用了此方法:

JavaScript 和 HTML 代码:

var getId  = 'tag:example.com,1999:blog-124938250235712450.post-092348192357990021',
    postID = getId.match(/post-\w*/),
    blogID = getId.match(/blog-\w*/);

/* Print Post ID */
$('#post-id').html(postID);

/* Print Blog ID */
$('#blog-id').html(blogID);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
Getting Post ID : <span id='post-id'></span>
<br/>
Getting Blog ID : <span id='blog-id'></span>

因此,输出为:
POST ID 类似于 post-092348192357990021
博客 ID 类似于 blog-124938250235712450

但是,我只需要每个数字,例如:
帖子 ID,如下所示:092348192357990021
博客 ID,如下所示:124938250235712450

希望大家能帮帮我
提前致谢!

最佳答案

使用正则表达式捕获组()并通过其索引[1]选择捕获。

如果您只需要数字,您也可以使用 \d 捕获数字。

var getId  = 'tag:example.com,1999:blog-124938250235712450.post-092348192357990021',
    postID = getId.match(/post-(\d*)/),
    blogID = getId.match(/blog-(\d*)/);

/* Print Post ID */
$('#post-id').html(postID[1]);

/* Print Blog ID */
$('#blog-id').html(blogID[1]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
Getting Post ID : <span id='post-id'></span>
<br/>
Getting Blog ID : <span id='blog-id'></span>

关于javascript - 如何使用 jquery 或 javascript 从字符串中查找完全不同的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49059755/

相关文章:

html - 在 Twitter Bootstrap 中,当我最小化我的窗口时,我的旋转木马失去宽度和高度以平衡,这很好但随后留下了巨大的差距

Javascript:字符串字符的行

javascript - 返回 undefined variable

javascript - 如何在 JavaScript 中访问模型中列表的属性?

php - 拥有非常简单的CMS系统

javascript - setInterval 函数在程序中只调用一次

html - 音频播放器中的html/hta多个文件

javascript - jquery ajax问题。 .done() 函数中的奇怪行为

javascript - 使用 github api 在 javascript/jquery 中创建链接

jQuery UI - 在 DOM 中移动可放置元素