0

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

2

2 Answers 2

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])

Sign up to request clarification or add additional context in comments.

3 Comments

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 !!
@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 = "(.*)";/)
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 ?!
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 .

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.