Regex get html input value dynamic tag position

十年热恋 提交于 2019-12-24 19:13:54

问题


I've question about how to get value from input html when the tag position is dynamic. This is the example input:

<input type="text" autocomplete="" name="confirmedEmailAddress" id="emailaddress" maxlength="60" value="fname.lname@gmail.com" class="txt-box  txtbox-m "  aria-describedby="" aria-required="true" disabled="disabled">

<input type="text" autocomplete="" value="fname.lname@gmail.com" name="confirmedEmailAddress" id="emailaddress" maxlength="60" class="txt-box  txtbox-m "  aria-describedby="" aria-required="true" disabled="disabled">

This is what I'm able to do, but can not detect if tag value position before id position like in the second example.

/<input(?:.*?)id=\"emailaddress\"(?:.*)value=\"([^"]+).*>/i

回答1:


You can try this:

<input.*?(?:id=\"emailaddress\".*?value=\"([^"]+)|value=\"([^"]+).*?id=\"emailaddress\")[^>]*>

Demo

The matches are shown on the right pane (not visible on test string for some reason). Since there's a |, you'll have to check both group 1 and group 2.

As many pointed out though, this is not something that should be done with regular expressions if you can avoid it.




回答2:


You can use "Beautiful Soup" module.

from bs4 import BeautifulSoup
html = '''<input type="text" autocomplete="" name="confirmedEmailAddress" id="emailaddress" maxlength="60" value="fname.lname@gmail.com" class="txt-box  txtbox-m "  aria-describedby="" aria-required="true" disabled="disabled">
        <input type="text" autocomplete="" value="fname.lname@gmail.com" name="confirmedEmailAddress" id="emailaddress" maxlength="60" class="txt-box  txtbox-m "  aria-describedby="" aria-required="true" disabled="disabled">'''
soup = BeautifulSoup(html,"lxml")
print(soup.find(id="emailaddress")['value'])

In front end app,can you use jquery.js?

$("#emailaddress").val()

document.getElementById('emailaddress').value


来源:https://stackoverflow.com/questions/49092484/regex-get-html-input-value-dynamic-tag-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!