简体   繁体   English

如何在页面上查找符号并在JS中的两个符号之间获取值?

[英]How to find symbols on a page and get a values between the two symbols in JS?

Some page have inserted secret text and JS script on this page should get a value between two characters, for example, between characters @@. 某些页面已插入秘密文本,并且此页面上的JS脚本应该在两个字符之间(例如,在字符@@之间)获得一个值。 In this variant @@569076@@ - 569076 should receive. 在此变体中,@@ 569076 @@-应该收到569076。

Here is what i've tried: 这是我尝试过的:

<div id="textDiv"></div>
<script>
    var markup = document.documentElement.outerHTML;
    var div = document.getElementById("textDiv");
    div.textContent = markup.match(/@@([^@]*)@@/);
    var text = div.textContent;
</script> 

But nothing displayed 但是什么也没显示

you should put your text/code into a variable. 您应该将文本/代码放入变量中。 You can replace body selector by yours. 您可以用自己的身体选择器代替。

Plain JS: 普通JS:

var html = document.getElementsByTagName('body')[0].innerHtml,
    match = html.match(/@@(\d+)@@/),
    digits;

if (match) {
    digits = match[1];
}

if you are using jQuery and like shortands: 如果您使用的是jQuery,并且喜欢短裤:

var digits, match;

if (match = $('body').html().match(/@@(\d+)@@/)) {
    digits = match[1];
}

The problem is that the match function returns an array of matched values and textContent will only accept a string. 问题在于match函数返回匹配值的数组,而textContent只接受一个字符串。

So you have to select which array item you want to use for the textContent assignment: 因此,您必须选择要用于textContent分配的数组项:

div.textContent = markup.match(/@@(\d+)@@/)[1];

Note we selected the first captured item using the [1] from the matches array. 请注意,我们使用matchs数组中的[1]选择了第一个捕获的项目

If the string has spaces in, just add a space in the list of matched items like so: 如果字符串中有空格,只需在匹配项列表中添加空格,如下所示:

var markup =  '@@56 90 76@@';
alert( markup.match(/@@([\d ]*)@@/)[1]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM