-1

here is the regex demo

the REGULAR EXPRESSION

getObj\("Frm_Logintoken"\).value = "(.*)";

this the TEST STRING

getObj("Frm_Logintoken").value = "3";

i want to get that number only "3" without quotes

it's in the Group 1 of the matches but i don't know how to get it from that group .

i can't var myString = "something format_abc";

because i am doing this to get the value that i don't know !!

And testing this in console results

var test = /getObj("Frm_Logintoken").value = "(.*)";/g

undefined

console.log(test1); undefined undefined

the same question but in a different way and detailed still unanswered

i have tried

getObj\("Frm_Logintoken"\).value = "(.*)";`.match(/getObj\("Frm_Logintoken"\).value = "(.*)";/)[1]

it give me this "(.*)" not the wanted value !!!

some notes

1-that value isn't static

2- i want to make the code works automatic so fetching the line "getObj("Frm_Logintoken").value = "3";" from the page code manually is unwanted thing.

3- i want to make an auto login script without any User intervention.

4- if you still don't understand the question see the links pls

thanks

Avi Meltser
  • 387
  • 3
  • 9
mina nageh
  • 143
  • 1
  • 2
  • 10
  • 1
    Possible duplicate of [How do you access the matched groups in a JavaScript regular expression?](https://stackoverflow.com/questions/432493/how-do-you-access-the-matched-groups-in-a-javascript-regular-expression) – Code Maniac Jun 08 '19 at 08:12
  • @CodeManiac it's not i have already looked there and found no suitable answer !! – mina nageh Jun 08 '19 at 08:39

2 Answers2

0

You can access group by accessing index of matched value

let str = `getObj("Frm_Logintoken").value = "3";`

let op = str.match(/getObj\("Frm_Logintoken"\).value = "(.*)";/)

console.log(op[1])
Code Maniac
  • 33,907
  • 4
  • 28
  • 50
  • dude haven't you seen this in the post ? "i can't var myString = "something format_abc"; because i am doing this to get the value that i don't know !!" ???? how i do var it while i don't even know it's value !!!!!!! i am trying to get it !! – mina nageh Jun 08 '19 at 08:46
  • @minanageh well how does that makes any difference, instead of keeping in variable you can directly do `'getObj("Frm_Logintoken").value = "3";'.match(/getObj\("Frm_Logintoken"\).value = "(.*)";/)` – Code Maniac Jun 08 '19 at 08:58
  • yes it make ..... if there is a one that i don't write the value in this what i need to grab the value from the page source code from the line getObj("Frm_Logintoken").value = ............... if this possible 'getObj("Frm_Logintoken").value = "gets me the value from the exact line before equals sign from the page sorce code";'.match(/getObj\("Frm_Logintoken"\).value = "(.*)";/) ............ is this clear enough ? if i set the value by myself then why the ... i don't use that number directly in the auto login code ?! – mina nageh Jun 08 '19 at 10:12
0

you must declare the string first !

so if you are trying to get the value from the current page html code you can just

let str = document.body.innerHTML;
let pattern =/\bgetObj\("Frm_Logintoken"\)\.value = "([^"]+)";/;
console.log(str.match(pattern)[1]); 

and if you are trying to fetch the html string from other page using something like XMLHttpRequest

you can do this

let str = (http.responseText);

the full code :

const http = new XMLHttpRequest();
const url = 'http://page/';
http.open('get', url, false);
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

  http.onreadystatechange = function ()
    {
        if(http.readyState === 4)
        {
            if(http.status === 200 || http.status == 0)
            {
                let str = (http.responseText);
                let pattern =/\bgetObj\("Frm_Logintoken"\)\.value = "([^"]+)";/;
                let results = str.match(pattern)[1];
                    console.log(results);
            }

        }
    }

http.send();

Hope you understand and make a Clearer question next time and write your really point of the question and the use of the wanted fix .

mina nageh
  • 143
  • 1
  • 2
  • 10