我有一个这样的字符串:
<script>This String may contain other JS tags in between </script>
我的要求是从字符串中删除开始和结束脚本标签,如果字符串之间有其他标签,则不应删除这些标签。
我怎样才能在 Perl 中做到这一点?
我有一个这样的字符串:
<script>This String may contain other JS tags in between </script>
我的要求是从字符串中删除开始和结束脚本标签,如果字符串之间有其他标签,则不应删除这些标签。
我怎样才能在 Perl 中做到这一点?
试试下面的 perl one liner:
perl -lpe "s/<\/?script>//g" inputfile
在 perl 中:
$string =~ s!<script[^>]*>|.*</\s*script>!!g;
您可以尝试以下代码来删除开始和结束脚本标签。
"<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
在 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