从输入标签获取名称和值 [英] Get name and value from the input tag

查看:85
本文介绍了从输入标签获取名称和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在你说哦不,不要再在这里之前,我要说明我的情况。我正在解析HTML输出的一部分,我唯一感兴趣的是每个 name 和属性< input / >标记。 HTML实际上是HTML片段,可能不是格式良好的。我没有DOM或HTML解析器,我也不试图解析嵌套元素。问题是我不知道属性的顺序或数量,因此它可能是< input name =foovalue =boo/> < input type =hiddenname = foo> < input id = blah value ='boo'src =image.pngname =footype =img/>

是否有一个正则表达式可以使我得到 name value 属性按可预测的顺序?我不会问这个问题是否可以假设 name 属性总是在 value 之前,但不幸的是这不是case

解决方案

要获取名称和<$ c $的值c> value 添加到同一个捕获组中,不管顺序如何,您可以尝试

 <输入(?= ['>] * name = [''']([^''] *)|)(?= [^>] * value = [''']([^'] 

如果您的regex实现支持lookahead。
这个假设值是引用的。


Before you say "oh no, not again" here I'm stating my case. I'm parsing part of HTML output and the only thing I'm interested in is name and value attributes of each <input/> tag. HTML is actually HTML fragment, may not be well-formed. I don't have DOM or HTML parser and I don't try to parse nested elements anyway. The problem is that I don't know the order or number of attributes so it could be <input name="foo" value="boo"/> or <input type="hidden" name=foo> or <input id=blah value='boo' src="image.png" name="foo" type="img"/>.

Is there a single regular expression that would get me values of name and value attribute in predictable order? I wouldn't have asked the question if I could assume that name attribute always precedes value but unfortunately this is not the case

解决方案

To get the values of name and value into the same capturing group, regardless of order, you could try

<input (?=[^>]* name=["']([^'"]*)|)(?=[^>]* value=["']([^'"]*)|)

if your regex implementation supports lookaheads. This assumes that the values are quoted.

这篇关于从输入标签获取名称和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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