0

I have always sucked at regular expressions and now I'm stuck with a problem. I am loading a webpage onto my AIR application now what i want to do is, I want to locate the text inside a certain pattern. See below.

<html>
<head></head>
<body>
Blah blah blah..
   Blah blah var my_variable = "this is a value";
</body>
</html>

On the above example, I want to get this is a value without the quotes. That value is in a pattern of var[space]my_variable[space]=[space]"content"; How do I extract JUST the value?

Jose Luis
  • 3,307
  • 3
  • 36
  • 53
Registered User
  • 8,706
  • 9
  • 32
  • 40

2 Answers2

1

Try this one:

/var [^ ]* = "([^"]*)"/

Which means:

var
space
everything but a space a certain amount of times
space
=
space
"
everything but a " a certain amount of times (caught)
"
Kodiak
  • 5,978
  • 17
  • 35
1

Pretty simple really. Extracting just the value requires that you use a capture group in the regex to match the contents between the quotes and then fetch the value of that capture group after a successful match. Assuming that the quoted string being assigned to the variable may contain escaped characters (including escaped double quotes), then the following tested code should work:

var re = /\bvar\s+my_variable\s+=\s+"([^"\\]*(?:\\[\Ss][^"\\]*)*)"/;
var results = text.match(re);
var content = results ? results[1] : null;
ridgerunner
  • 33,777
  • 5
  • 57
  • 69