doujianzhi3358 2010-09-15 08:55
浏览 42
已采纳

正则表达式,可以区分HTML标记中包含的PHP标记

I need a javascript regex that can distinguish between PHP tags in HTML tags and PHP tags outside of HTML tags.

e.g.

<input type="text" <? print '1'; ?> value="<? print '2'; ?>">

<? print '3';?>

So I need a regex to pull out:

<? print '1'; ?> and <? print '2'; ?>

And another regex to pull out:

<? print '3';?>

At the moment I have this regex which pulls out all PHP tags regardless of where they are:

/
?<\?(php)?(\s|[^\s])*?\?>
?/ig
  • 写回答

3条回答 默认 最新

  • doulou1989 2010-09-15 09:00
    关注

    This is a very complex thing to do, and I very much doubt it can be solved by regular expressions. This does depend to some degree on how complex the PHP that you want to extract is, but there are many cases to consider:

    <?=max($a, $b);?>
    <? echo max($a, $b); ?>
    <?php echo ($a > $b) ? 'yes' : 'no'; ?>
    <div><p><?php echo '</p>'; ?></div>
    

    Why do you need to do this with JavaScript and regular expressions?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器