Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing Javascript using Ruby code [closed]

I'm writing a test code in Ruby and trying to parse a HTML source file of a website. It has a JavaScript variable which I can use to compare it against other values. For example:

<script type="text/javascript" language="JavaScript">
  function GetParam(name) {   
    var req_var = {
      a: 'xyz', 
      b: 'yy.com', 
      c: 'en', 
      d:0, 
      e: 'y'
     };
  }
</script>

Here I want to extract the variable req_var from this function. Is it possible to do that? If so can anyone please help me with that?

like image 968
rubytester Avatar asked Feb 09 '11 00:02

rubytester


2 Answers

javascript parser in ruby

  • rbnarcissus
  • Rkelly
  • johnson
like image 152
karlcow Avatar answered Nov 15 '22 15:11

karlcow


You could use a regular expression to parse it out like this:

k = "function GetParam(name) { var req_var = { a: 'xyz' , b: 'yy.com' , c: 'en' , d:0 , e: 'y'}; }"
variable = k.match(/var\s+req_var\s+=\s+(.*?);/m)[1]
p variable

=> "{ a: 'xyz' , b: 'yy.com' , c: 'en' , d:0 , e: 'y'}"
like image 42
Pan Thomakos Avatar answered Nov 15 '22 14:11

Pan Thomakos