我在HTML中有一个div。它具有此值:“显示31项中的1到25 ”。数字31可能会有所不同。我想使用JavaScript在一些变量中获得31。最好的方法是什么?
Javascript:
var x = document.getElementById('available-questions_info').innerHTML;
alert("CURRENT VALUE IS "+x);
HTML:
<div class="dataTables_info" id="available-questions_info">Showing 1 to 25 of 31 entries</div>
您可以使用正则表达式捕获它:
test("Showing 1 to 25 of 31 entries");
test("Showing 1 to 25 of 53 entries");
test("Showing 1 to 1 of 1 entry");
function test(str) {
var rex = /(\d+) (?:entry|entries)/;
var match = rex.exec(str);
if (match) {
snippet.log(match[1]); // "31", etc.
} else {
snippet.log("Didn't match");
}
}
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="//tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
/(\d+) (?:entry|entries)/
表示“捕获一个或多个数字,后跟一个空格和单词” entry”或” entry””。捕获的文本可用作结果匹配数组中的条目#1。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句