Reputation: 893
I need to get the assigning variable name and value from javascript code in HTML.
For example, there is html code:
<html>
<head>~~</head>
<body>
<div>contents</div>
<script>
var value1 = 55;var value2= 27;
var value3 = 'T';var value4 = "FIT#$%SIZE";
var value5 = '{\"P00000WJ000E\":{\"stock_price\":\"0.00\",\"use_stock\":true,\"use_soldout\":\"T\",\"is_display\":\"T\",\"is_selling\":\"T\",\"option_price\":79000,\"option_name\":\"FIT#$%SIZE\",\"option_value\":\"NOBLE-44\",\"stock_number\":26,\"option_value_orginal\":[\"NOBLE\",\"44\"],\"use_stock_original\":\"T\",\"use_soldout_original\":\"T\",\"use_soldout_today_delivery\":\"F\",\"is_auto_soldout\":\"F\",\"is_mandatory\":\"T\",\"option_id\":\"000E\",\"is_reserve_stat\":\"N\",\"item_image_file\":null,\"origin_option_added_price\":\"0.00\"}}';
var value6 = '1';
var value7 = 'string;must-catch';
var value8 = 8;
var value9 = 'S';
var value10 = 'T';
</script>
</body>
</html>
expected things are:
result = {
value1: 55,
value2: 27,
value3: 'T',
value4: "FIT#$%SIZE",
...
}
In my environment, another parser tool cannot be used without Regex. So trying to almost covered things like:
var\s(\w+)\s?\=\s?(\d+|\"[^;]*\"|\'[^;]*\')\s?\;
The result has appeared but, I also need to capture semicolon included string. Any ideas?
Upvotes: 0
Views: 73
Reputation: 163577
Instead of not matching a semicolon using [^;]*
you could match not a newline instead [^\n']
and add the single or double quote to it [^\n"]
var\s(\w+)\s?\=\s?(\d+|\"[^\n"]*\"|\'[^\n']*\')\s?\;
^^^ ^^^
If you do want to match a newline you can omit it from the negated character class like [^"]*
and [^']*
.
Upvotes: 1