创建一个Javascript RegExp以在HTML/php模板中查找开始标签 [英] Create a Javascript RegExp to find opening tags in HTML/php template

查看:79
本文介绍了创建一个Javascript RegExp以在HTML/php模板中查找开始标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个Javascript HTML/php解析器,该解析器将从HTML/php源提取所有打开的标签,并返回标签和属性的类型及其值,同时监视值/属性是否应从静态文本或php变量求值.问题是当我尝试编写Javascript RegExp模式,更具体地说是某些罕见情况时.我能够提出的RegExp可能涉及负面的回溯问题(以应付php结束标记-匹配不带问号的结束括号)或在某些情况下失败.幕后版本看起来像:

I'm trying to write a Javascript HTML/php parser which would extract all opening tags from a HTML/php source and return the type of tag and attributes with their values while at the same time monitoring whether the values/attributes should be evaluated from static text or php variables. The problem is when I try to compose the Javascript RegExp pattern and more specifically certain rare cases. The RegExp I was able to come up with either involve negative lookbehind (to cope with the closing php tag - that is to match a closing bracket that is not preceded by a question mark) or fails in certain cases. The lookbehind version looks like:

<[a-zA-Z]+.*?(?<!\?)>

...并且工作正常,但我的情况除外,该情况必须避免使用向后看.一个更友好的Javascript版本将是:

...and works perfect except for my case which must avoid using lookbehind. A more Javascript friendly version would be:

<[a-zA-Z]+((.(?!</)(?!<[a-zA-Z]+))*)?>

...除了在这种情况下有效:

...which works except in this case:

<option value="<?php echo $img; ?>"<?php echo ($hpb[$i]['image_filename']==$img?' selected="selected"':''); ?>><?php echo $img; ?></option>

我要解决的问题完全搞砸了吗,或者在我的情况下,真正需要回头看吗?任何帮助将不胜感激.

Am I approaching the problem completely messed up or is the lookbehind really necessary in my case? Any help is greatly appreciated.

推荐答案

只需使用[^?]确保'>'之前的最后一个字母不是?.无需先行或落后.

Just make sure the last letter before the '>' is not a ?, using [^?]. No lookaheads or -behinds needed.

<[a-zA-Z](.*?[^?])?>

括号和最后一个?也要匹配<b>之类的标签.

the parentheses and the last ? is to also match tags like <b>.

编辑该解决方案不适用于没有属性的单个字符标签.所以这是一个做的事:

EDIT The solution didn't work for single character tags without attributes. So here is one that does:

<[a-zA-Z]+(>|.*?[^?]>)

这篇关于创建一个Javascript RegExp以在HTML/php模板中查找开始标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆