1

antisamyポリシーファイルに次の設定があります:

古いYouTubeオブジェクト:

<object width="1280" height="720">
<param 
    name="movie" 
    value="http://www.youtube.com/v/Hl-zzrqQoSE
           ?version=3
           &amp;hl=en_US
           &amp;rel=0">
</param>
<param name="allowFullScreen" value="true">
</param>
<param name="allowscriptaccess" value="always">
</param>
<embed src="http://www.youtube.com/v/Hl-zzrqQoSE
            ?version=3
            &amp;hl=en_US
            &amp;rel=0" 
       type="application/x-shockwave-flash" 
       width="1280" 
       height="720" 
       allowscriptaccess="always" 
       allowfullscreen="true">
 </embed>
 </object>

AntiSamy構成:

 <common-regexps>
     <regexp name="YouTubeURL" value="(\s)*(http(s?)://)www.youtube.com/v/[\p{L}\p{N}]+[\p{L}\p{N}\p{Zs}\.\#@\$%\+&amp;;:\-_~,\?=/!]*(\s)*"/>
 ....

<!-- Tags related to YouTube -->
<tag name="object" action="validate">
<attribute name="height"/>
<attribute name="width"/>
<attribute name="type">
    <literal-list>
        <literal value="application/x-shockwave-flash"/>
    </literal-list>
</attribute>
<attribute name="data">
    <regexp-list>
        <regexp name="YouTubeURL"/>
    </regexp-list>
</attribute>
</tag>
<tag name="embed" action="validate">
<attribute name="height"/>
<attribute name="width"/>
<attribute name="type">
    <literal-list>
        <literal value="application/x-shockwave-flash"/>
    </literal-list>
</attribute>
<attribute name="allowfullscreen">
    <regexp-list>
        <regexp name="boolean"/>
    </regexp-list>
</attribute>
<attribute name="allowscriptaccess">
    <literal-list>
        <literal value="always"/>
    </literal-list>
</attribute>
<attribute name="src">
    <regexp-list>
        <regexp name="YouTubeURL"/>
    </regexp-list>
</attribute>
<attribute name="movie">
    <regexp-list>
        <regexp name="YouTubeURL"/>
    </regexp-list>
</attribute>
</tag>

現在、iframeでの私の設定:

    <!-- Frame & related tags -->

    <tag name="iframe" action="remove"/>
    <tag name="frameset" action="remove"/>
    <tag name="frame" action="remove"/>

新しいYouTubeiframe:

<iframe 
    width="1280" 
    height="720" 
    <!--   src="https://www.youtube-nocookie.com/embed/Hl-zzrqQoSE"  -->
    src="https://www.youtube.com/embed/Hl-zzrqQoSE" 
    frameborder="0" 
    allowfullscreen>
</iframe>

iframeのコードは次のようになります。

<tag name="iframe" action="validate">
        <attribute name="height"/>
        <attribute name="width"/>
        <attribute name="frameborder"/>
        <attribute name="src">
            <regexp-list>
                <regexp name="YouTubeURL"/>
            </regexp-list>
        </attribute>

        <attribute name="allowfullscreen">
            <regexp-list>
                <regexp name="boolean"/>
            </regexp-list>
        </attribute>
</tag>

次のような新旧のリンクを受け入れるように、正規表現をどのように変更しますか。

    https://www.youtube-nocookie.com/embed/Hl-zzrqQoSE
    https://www.youtube.com/embed/Hl-zzrqQoSE
    https://www.youtube.com/v/Hl-zzrqQoSE
    http://www.youtube-nocookie.com/v/Hl-zzrqQoSE?version=3&amp;hl=en_US&amp;rel=0
    http://www.youtube.com/v/Hl-zzrqQoSE?version=3&amp;hl=en_US&amp;rel=0"
4

1 に答える 1

2
\s*(https?://)www.youtube(-nocookie)?.com/(?:v|embed)/[\p{L}\p{N}]+[\p{L}\p{N}\p{Zs}.#@$%+&;:_~,?=!/-]*\s*

私は自由に不要なキャプチャグループ、エスケープ、キャラクターを削除しました。

私は個人的に次のようなものを使用しますが:

\s*(https?://www.youtube(?:-nocookie)?.com/(?:v|embed)/([a-zA-Z0-9-]+).*)

これにより、YouTubeのURL全体が一致グループ0に、ビデオIDが一致グループ1に配置されます。また、YouTubeのURLにUnicode文字が含まれていない場合に、Unicodeプロパティを使用することはあまり意味がありません。

デモ: http: //rubular.com/r/jv4zO9ys2L

于 2011-10-04T22:59:57.960 に答える