I'm looking to mine out some goodies from a multi-line string of text. I'm comfy doing regex in Perl (though I'm sure there is a better way than my code below), but don't really see how to use a marked string in the regexp as part of the newSubStr in Javascript. Is there a way or am I stuck running multiple replaces on this to ditch the audio and source lines?
$_ = <<END;
<audio controls="controls" preload="metadata">
<source src="01.mp3" type="audio/mpeg">
<source src="01.ogg" type="audio/ogg">
Stuff
Default: <a href="01.mp3">>>download</a>
</audio>
END
s#.*<source.*?>.*?\n(.*)\n</audio>.*#$1#s;
print "[$_]\n";
Multiples regex in (my limited) Javascript might like this:
// We're really dependent on the HTML layout for line feeds
// so watch out.
var line = aElems[i].innerHTML.replace(/.*?audio.*?\n/gm, '');
var line2 = line.replace(/.*<source.*?\n/mg, '');
console.log(line2);
From reading both your questions it sounds like what you really want is to make the parent tag of your audio tag contain the innerHTML of your audio tag with the source elements removed.
A regexp would be error prone especially when you can use the DOM to get the same results with less effort.
var audio_tag = ...;
var elements_to_delete = audio_tag.getElementsByTagName('source');
for (var idx = elements_to_delete.length - 1; idx >= 0; --idx) {
audio_tag.removeChild( elements_to_delete[idx] );
}
audio_tag.parentNode.innerHTML = audio_tag.innerHTML;
Although you say you want to use JavaScript, I thought I'd show you the non-regex Perl approach. The HTML::TokeParser::Simple makes it pretty easy:
use HTML::TokeParser::Simple;
my $p = HTML::TokeParser::Simple->new( *DATA );
TOKEN: while( my $token = $p->get_token ) {
if( $token->is_start_tag( 'audio' ) ){
AUDIO: while( my $t = $p->get_token ) {
next AUDIO if $t->is_tag( 'source' );
last AUDIO if $t->is_end_tag( 'audio' );
print $t->as_is;
}
next TOKEN;
}
print $token->as_is;
}
__DATA__
<html>
<head><title>Test</title></head>
<body>
<p>Keep this</p>
<audio controls="controls" preload="metadata">
<source src="01.mp3" type="audio/mpeg">
<source src="01.ogg" type="audio/ogg">
Stuff
Default: <a href="01.mp3">>>download</a>
</audio>
<p>Keep this</p>
</body>
</html>
This gives:
<html>
<head><title>Test</title></head>
<body>
<p>Keep this</p>
Stuff
Default: <a href="01.mp3">>>download</a>
<p>Keep this</p>
</body>
</html>
There are other Perl modules that will correctly parse HTML and play with the structure, too.
For the JavaScript side, why don't you just replace the HTML? I know you asked a related question about this. It seems to me that something else should be generating the content inside the audio and should be able to give you something you like in this case. I'd back up a step and work on that. Or, you can explain much more about your problem.
perl
- NoName 2012-04-04 20:55