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?
javascript parser in ruby
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'}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With