开发者

Replace all instances of a character inside a specific HTML element/tag

Adobe Dreamweaver's Search 开发者_如何学JAVA& Replace feature offers to limit its scope to "only inside of the xxxx HTML tag".

I want to do this with Mac OS X' command line (so will do anything that comes bundled with it).

For example, how do I remove all instances of the character "a" inside all <h1> with the command line?


You can use unix's sed command (which is available on mac too). e.g.

$ cat foo.xml 
<h1>axyzabca</h1>
<a href="foo.com">abc</a>
<h1>aa</h1>
<h1>a</h1>
<h1></h1>

$ cat foo.xml | sed 's/<h1>a*\([^a]*\)a*\([^a]*\)a*<\/h1>/<h1>\1\2<\/h1>/g' 
<h1>xyzbc</h1>
<a href="foo.com">abc</a>
<h1></h1>
<h1></h1>
<h1></h1>

(foo.xml is a sample input which covers common test cases)


This isn't much of a one-liner, but --

perl -ni -e '
    $/ = undef; $x = <>; $y = "";
    while ($x =~ m#^(.*?<h1>)(.*?)(</h1>)(.*)$#si) {
      $x = $4; $y .= $1; $c = $3;
      ($b = $2) =~ s/a/(something else)/g;
      $y .= $b . $c;
    } print $y . $x;
' filename.html
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜