如何使用Perl选择性地修改HTML文档中脚本标签的src属性? [英] How can I selectively modify the src attributes of script tags in an HTML document using Perl?
本文介绍了如何使用Perl选择性地修改HTML文档中脚本标签的src属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在Perl中编写一个正则表达式,为所有src加上[perl] texthere [/perl]前缀,例如:
I need to write a regular expression in Perl that will prefix all srcs with [perl]texthere[/perl], like such:
<script src="[perl]texthere[/perl]/text"></script>
有帮助吗?谢谢!
推荐答案
使用适当的解析器,例如 HTML :: TokeParser ::简单:
Use a proper parser such as HTML::TokeParser::Simple:
#!/usr/bin/env perl
use strict; use warnings;
use HTML::TokeParser::Simple;
my $parser = HTML::TokeParser::Simple->new(handle => \*DATA);
while (my $token = $parser->get_token('script')) {
if ($token->is_tag('script')
and defined(my $src = $token->get_attr('src'))) {
$src =~ m{^https?://}
or $token->set_attr('src', "[perl]texthere[/perl]$src");
}
print $token->as_is;
}
__DATA__
<script src="/js/text.text.js/"></script>
And at the same time, ignore scrs that begin with http, as such:
<script src="https://websitewebsitewebsite"></script>
输出:
<script src="[perl]texthere[/perl]/js/text.text.js/"></script>
And at the same time, ignore scrs that begin with http, as such:
<script src="https://websitewebsitewebsite"></script>
这篇关于如何使用Perl选择性地修改HTML文档中脚本标签的src属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文