doushishi6513 2014-12-22 06:47
浏览 87

匹配所有js代码而不是注释的正则表达式

The following regex is supposed to match all js:

  • conditional js (OK: already working)
  • add js script (OK: already working)
  • in line js (OK: already working)
  • but not commented Js ( need to be implemented )

I would like to exclude all js that is commented ( but preserve the one that inside a conditional comment

Here the current regex:

^<!--(\[if lt IE.*?\])>[\s\S]*?<script(\b[^>]*?>)([\s\S]*?)<\/script>[\s\S]*?<!\[endif\]-->$|^<script(\b[^>]*?>)([\s\S]*?)<\/script>$

So skip the match for this kind of text:

<!--
<script type="text/javascript">
SOME JS
</script> 
-->

OR

<!--
<script src='...' type="text/javascript"></script> 
-->

Conditional comment are usually in this form:

<!--[if lt IE]>
<script type="text/javascript">
Some JS
</script>
<![endif]-->

Is it possible to do all the above in one regex ?
Since this is my first regex, if you have any other suggestion is welcome.

Thanks

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)