Astraport Astraport - 4 years ago 132
Javascript Question

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 @@. In this variant @@569076@@ - 569076 should receive.

Here is what i've tried:

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

But nothing displayed

Answer Source

The problem is that the match function returns an array of matched values and textContent will only accept a string.

So you have to select which array item you want to use for the textContent assignment:

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

Note we selected the first captured item using the [1] from the matches array.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download