我必须解析这个Javascript字符串以增加字体大小,行高和宽度,因为文本目前对我们的移动用户来说太小了。不幸的是,我无法控制这个可怕的字符串混乱,因此,无法以任何方式修改输入。我基本上是在破解旧的iFrame。
nftext=new String(''+
'<html>'+
'<head>'+
'<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">'+
'<meta http-equiv=Content-Type content="text/html; charset=iso-8859-1">'+
'<style type="text/css">'+
'html,body {height:100%; width:100%; overflow:hidden; }'+
' html {overflow:hidden; border:solid black 1pt;}'+
' body {margin:0px; padding:0px; font-family:Arial,Helvetica,sans-serif; font-size:8pt;}'+
' td {font-size: 8pt;}'+
'</style>'+
'</head>'+
'<body>'+
'<div style="position:relative; border:solid black 1pt; '+
' padding-top:2px; padding-left:3px; padding-right:2px; padding-bottom:0px; margin-left:1px; margin-top:1px; margin-bottom:0px; margin-right:1px;">'+
'<table valign="top" style="width:210px; padding:0px; border-collapse:collapse; margin:0px;">'+
'<tr>'+
'<td style="padding:0px; width:11px; font-size:1px;"></td>'+
'<td style="padding:0px; width:42px; font-size:1px;"></td>'+
'<td style="padding:0px; width:32px; font-size:1px;"></td>'+
'<td style="padding:0px; width:32px; font-size:1px;"></td>'+
'<td style="padding:0px; width:32px; font-size:1px;"></td>'+
'<td style="padding:0px; width:29px; font-size:1px;"></td>'+
'<td style="padding:0px; width:32px; font-size:1px;"></td>'+
'</tr>'+c1+' NUTRITION FACTS '+c2+
'<tr>'+
'<td colspan=7>'+
'<span style="font-size:17pt; letter-spacing:'+((browserType=="firefox")?'2.5px;':'2.25px;')+
' line-height:80%; margin-top:-10px; text-align:justify;"><b style="font-weight:600;">Nutrition Facts</b></span></td>'+
'</tr>'+
c1+' SERVING SIZE '+c2+
'<tr><td colspan=7 style="line-height:10.0pt; border-bottom:solid black 8pt;">Serving Size ~servsize~</td></tr>'+
c1+' AMOUNT PER SERVING '+c2+
'<tr>'+
'<td colspan=7 style="font-size:6.0pt; line-height:6.0pt; border-bottom:solid black 1pt;"><b>Amount per Serving</b></td>'+
'</tr>'+
c1+' CALORIES / CALORIES FROM FAT '+c2+
'<tr>'+
'<td colspan=3 style="line-height:10.0pt; border-bottom:solid black 3pt;"><b style="font-weight:600;">Calories</b> ~c~</td>'+
'<td colspan=4 align=right style="line-height:10.0pt; border-bottom:solid black 3pt;">Calories from Fat ~cf~</td>'+
'</tr>'+
c1+' DAILY VALUE '+c2+
'<tr>'+
'<td colspan=7 align=right style="font-size:6.0pt; line-height:6.0pt; border-bottom:solid black 1pt;"><b>% Daily Value</b></td>'+
'</tr>'+
c1+' TOTAL FAT '+c2+
'<tr>'+
'<td colspan=4 style="line-height:10pt; border-bottom:solid black 1.0pt"><b style="font-weight:600;">Total Fat</b> ~fat~g</td>'+
'<td colspan=3 align=right style="line-height:10pt; border-bottom:solid black 1pt;"><b style="font-weight:600;">~fatp~</b>%</td>'+
'</tr>'+
c1+' SAT FAT '+c2+
'<tr>'+
'<td style="width:11px; line-height:10pt;"> </td>'+
'<td colspan=4 style="line-height:10pt; border-bottom:solid black 1.0pt;">Saturated Fat ~sfat~g</td>'+
'<td colspan=2 align=right style="line-height:10pt; border-bottom:solid black 1.0pt;"><b style="font-weight:600;">~sfatp~</b>%</td>'+
'</tr>'+
c1+' TRANS FAT '+c2+
'<tr>'+
'<td style="width:11px; line-height:10pt; border-bottom:solid black 1.0pt;"> </td>'+
'<td colspan=6 style="line-height:10pt; border-bottom:solid black 1.0pt;"><i>Trans</i> Fat ~tfat~</td>'+
'</tr>'+
c1+' CHOLESTEROL '+c2+
'<tr >'+
'<td colspan=4 style="line-height:10pt; border-bottom:solid black 1.0pt;"><b style="font-weight:600;">Cholesterol</b> ~chl~mg</td>'+
'<td colspan=3 align=right style="line-height:10pt; border-bottom:solid black 1.0pt;"><b style="font-weight:600;">~chp~</b>%</td>'+
'</tr>'+
c1+' SODIUM '+c2+
'<tr>'+
'<td colspan=4 style="line-height:10pt; border-bottom:solid black 1.0pt;"><b style="font-weight:600;">Sodium</b> ~sod~mg</td>'+
'<td colspan=3 align=right style="line-height:10pt; border-bottom:solid black 1.0pt;"><b style="font-weight:600;">~sodp~</b>%</td>'+
'</tr>'+
c1+' TOTAL CARBOHYDRATES '+c2+
'<tr>'+
'<td colspan=6 style="line-height:10pt; border-bottom:solid black 1.0pt;"><b style="font-weight:600;">Total Carbohydrates</b> ~b~g</td>'+
'<td align=right style="line-height:10pt; border-bottom:solid black 1.0pt"><b style="font-weight:600;">~bp~</b>%</td>'+
'</tr>'+
c1+' DIETARY FIBER '+c2+
'<tr>'+
'<td style="line-height:10pt;"> </td>'+
'<td colspan=4 style="line-height:10pt; border-bottom:solid black 1.0pt;">Dietary Fiber ~df~g</td>'+
'<td colspan=2 align=right style="line-height:10pt; border-bottom:solid black 1.0pt;"><b style="font-weight:600;">~dfp~</b>%</td>'+
'</tr>'+
c1+' SUGARS '+c2+
'<tr>'+
'<td style="line-height:10pt; border-bottom:solid black 1.0pt;"> </td>'+
'<td colspan=6 style="line-height:10pt; border-bottom:solid black 1.0pt;">Sugars ~sug~g</span></td>'+
'</tr>'+
c1+' PROTEIN '+c2+
'<tr>'+
'<td colspan=7 style="line-height:10pt; border-bottom:solid black 8.0pt;"><b>Protein</b> ~pro~g</td>'+
'</tr>'+
c1+' VIT A/VIT C '+c2+
'<tr>'+
'<td colspan=3 style="line-height:10pt; border-bottom:solid black 1.0pt;">Vitamin A ~a~%</td>'+
'<td align=right style="line-height:10pt; border-bottom:solid black 1.0pt;"><b>•</b> </td>'+
'<td colspan=3 align=right style="line-height:10pt; border-bottom:solid black 1.0pt;">Vitamin C ~cp~%</td>'+
'</tr>'+
c1+' CALCIUM/IRON '+c2+
'<tr>'+
'<td colspan=3 style="line-height:10pt; border-bottom:solid black 1.0pt;">Calcium ~up~%</td>'+
'<td align=right style="line-height:10pt; border-bottom:solid black 1.0pt;"><b>•</b> </td>'+
'<td colspan=3 align="right" style="line-height:10pt; border-bottom:solid black 1.0pt;">Iron ~ip~%</td>'+
'</tr>'+
c1+' BASED ON '+c2+
'<tr>'+
'<td align=center valign=top style="line-height:8.0pt; padding-top:2px;">*</td>'+
'<td valign=top colspan=6 style="font-size:6.5pt; line-height:8.0pt; padding-top:2px;">Percent Daily Values are based on a 2,000 '+
'calorie diet.</td></tr></table></div>'+
'<div style="height:77px; font-size:7.5pt; line-height: 120%; margin-left:4px; margin-right:4px; margin-top:3px; margin-bottom:1px; vertical-align:top;">'+
'<b>~rname~</b><div style="line-height: 120%; font-size:7pt; margin-left:1px; margin-top:1px; margin-bottom:2px;">~rdesc~</div></div>'+
'<div style="z-index: 1; position:absolute; left:5px; right:2px; bottom:13px; font-size:7pt; color:#800000;">~allergen~</div>'+
'<div style="z-index: 0; position:absolute; text-align:center; left:0px; right:0px; bottom:0px; width:100%; font-size:7pt; color:#fff; background-color:#000; ">Move mouse or press Escape key to close</div>'+
'</body></html>');
我可以使用一组正则表达式来获取我需要的东西吗?目前我使用这个过于复杂的函数,我写的这给我带来了不可靠的结果。它正确地替换了一些属性,但对于其他属性,它将跳过替换它们,因为它在属性的第一个字母后面开始(例如:&#39; ine-height:&#39;&#39; ont-size: &#39)
。function findAndReplaceProperty( baseString, propertyName, replaceWith ){
var indices = getMatchIndexes(baseString, propertyName);
//replace each property
for(var i = 0; i < indices.length; i++){
//get original string
var beginningOfWord = indices[i]; //word starts here
var endOfWord = baseString.indexOf( ';' , beginningOfWord ); //css values always end at a semicolon
//actually grab css property
var strOriginal = baseString.substring(beginningOfWord, endOfWord+1);
//remove extraneous junk
strOriginal = strOriginal.replace('{', '');
strOriginal = strOriginal.replace('=', '');
strOriginal = strOriginal.replace('"', '');
strOriginal = strOriginal.trim();
//if strOriginal actually matches the property we want to replace
//replace it
if (replaceWith.substring(0,1) == strOriginal.substring(0,1)) {
console.log('replacing ' + strOriginal + ' with ' + replaceWith);
baseString = baseString.replace(strOriginal, replaceWith);
}
else{
console.log('did not replace ' + strOriginal);
}
}
return baseString;
}
//function getMatchIndexes from StackOverflow
function getMatchIndexes(str, toMatch) {
var toMatchLength = toMatch.length,
indexMatches = [], match,
i = 0;
while ((match = str.indexOf(toMatch, i)) > -1) {
indexMatches.push(match);
i = match + toMatchLength;
}
return indexMatches;
}
答案 0 :(得分:0)
首先提取样式标记的内容:
/\<style[^>]*\>([^<]+)\</
然后匹配每个元素的样式:
/([a-zA-Z,]+)[\s\n\r]+\{([^}]+)\}/
然后为每个结果匹配所有:
/([a-z]+)\s*:\s*([0-9a-zA-Z]+)\s*;/
答案 1 :(得分:0)
搞砸了 - 在花了太多时间之后,我决定使用这种方法。它不是......理想的,但效果还不错。
var replaced = popup.replace(/font-size:6.0pt;/g, 'font-size: 1em;');
replaced = replaced.replace(/font-size:6.5pt;/g, 'font-size: 1.25em;');
replaced = replaced.replace(/font-size:7pt;/g, 'font-size: .75em;');
replaced = replaced.replace(/font-size:7.5pt;/g, 'font-size: 1.5em;');
replaced = replaced.replace(/font-size:8pt;/g, 'font-size: 2em;');
replaced = replaced.replace(/font-size: 8pt;/g, 'font-size: 2em;');
replaced = replaced.replace(/font-size:17pt;/g, 'font-size: 3em;');
//increase width
replaced = replaced.replace(/width:210px;/g, 'width:800px;');
//increase line-height
replaced = replaced.replace(/line-height:6pt;/g, 'line-height:1em;');
replaced = replaced.replace(/line-height:6.0pt;/g, 'line-height:1em;');
replaced = replaced.replace(/line-height:8pt;/g, 'line-height:1.25em;');
replaced = replaced.replace(/line-height:8.0pt;/g, 'line-height:1.25em;');
replaced = replaced.replace(/line-height:10pt;/g, 'line-height:1.5em;');
replaced = replaced.replace(/line-height:10.0pt;/g, 'line-height:1.5em;');
console.log('final html: ' + replaced);