
代替の解決策が欲しいこのjQueryハック車輪の再発明を少し試してみましたが、基本的な *ix ツールを使用して、ワンライナーでこれを行うことができると思います。はるかに簡単で直接的な方法は、px で追加されたすべての数字を検索し\d+px
、いくつかの一致を見つけて定数で乗算することですが、\d
数字が先頭に付いていない場合にのみ一致し[^\d]\d+px
、置換/乗算を実行します。
最もシンプルでエレガントなソリューションが勝ちます。一致-乗算-置換をどのように実行しますか?
私は Vim を使用していますが、新しいアイデアにもオープンです。
答え1
bash-4.2$ cat map.htm
<map name="world">
<area shape="rect" coords="0%,0%,32%,32%" >
<area shape="rect" coords="8%,40%,50%,80%" >
<area shape="rect" coords="56%,4%,84%,18%" >
<area shape="rect" coords="74%,20%,90%,40%" >
<area shape="rect" coords="70%,42%,96%,62%" >
<area shape="rect" coords="0%,74%,70%,95%" >
</map>
bash-4.2$ perl -pe '$w=640;$h=480;s!(\d+)%,(\d+)%,(\d+)%,(\d+)%!($1*$w/100).",".($2*$h/100).",".($3*$w/100).",".($4*$h/100)!ge' map.htm
<map name="world">
<area shape="rect" coords="0,0,204.8,153.6" >
<area shape="rect" coords="51.2,192,320,384" >
<area shape="rect" coords="358.4,19.2,537.6,86.4" >
<area shape="rect" coords="473.6,96,576,192" >
<area shape="rect" coords="448,201.6,614.4,297.6" >
<area shape="rect" coords="0,355.2,448,456" >
</map>
もちろん、正規表現はHTMLを解析する方法ではないため、特定の状況では失敗するでしょう。SOで締結しかし、専用の HTML パーサーで解決すると、もはやワンライナーではなくなります。
しかし、座標ペアのようなプレーンテキストとの競合がないことが確実な場合は、次のように短縮することもできます。
perl -pe '$w=640;$h=480;s!(\d+)%,(\d+)%!int($1*$w/100).",".int($2*$h/100)!ge' map.htm