开发者

Finding contents inside a pattern? AS3

I have always sucked at regular expressions and now I'm st开发者_如何学Gouck 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?


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;


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)
"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜