0

我有一个这样的字符串:

<script>This String may contain other JS tags in between </script>

我的要求是从字符串中删除开始和结束脚本标签,如果字符串之间有其他标签,则不应删除这些标签。

我怎样才能在 Perl 中做到这一点?

4

4 回答 4

2

试试下面的 perl one liner:

perl -lpe "s/<\/?script>//g" inputfile
于 2014-09-05T19:33:36.037 回答
1

在 perl 中:

$string =~ s!<script[^>]*>|.*</\s*script>!!g;
于 2014-09-05T13:04:46.387 回答
0

您可以尝试以下代码来删除开始和结束脚本标签。

"<script>This String may contain other JS tags in between </script>".replace(/^<script>|<\/script>$/g, "");
'This String may contain other JS tags in between '

或者

"foo <script>This String may contain other JS tags in between </script> foo".replace(/^((?:(?!<script>).)*)<script>(.*?)<\/script>((?:(?!<script>).)*)$/g, "$1$2$3");
'foo This String may contain other JS tags in between  foo'

通过perl,

$ echo 'foo <script>This String may contain other JS tags in between </script> foo' | perl -pe 's/^((?:(?!<script>).)*)<script>(.*?)<\/script>((?:(?!<script>).)*)$/\1\2\3/g'
foo This String may contain other JS tags in between  foo
于 2014-09-05T13:00:52.357 回答
0

在 perl 中,您可以进行测试以检查它是否与您的标签匹配,然后进行替换。

#!/usr/bin/perl

use warnings; 
use strict;

my $string = '<script>This String may contain other JS tags in between </script>';

if ( $string =~ /^(<script>).*(<\/script>)$/ ) {
$string =~ s/$1|$2//g;
}
print $string, "\n";

这将打印:

This String may contain other JS tags in between
于 2014-09-05T13:27:09.830 回答