0

我有一个 input type="text" 字段我想从中获取 value 属性,但条件是输入标签的 id 必须包含“PlaceHolderSearchArea”这个词

输入字段的html是这样的

<input name="ctl00$PlaceHolderSearchArea$SearchBox$S633C1122_InputKeydummywords" type="text" value="something something" maxlength="200" id="ctl00_PlaceHolderSearchArea_SearchBox_S622C1022_InputKeywords" accesskey="S" title="something..." class="ms-sbplain" alt="something..." onkeypress="javascript: return S633C1122_OSBEK(event);" onfocus="if (document.getElementById('ctl00_PlaceHolderSearchArea_SearchBox_ctl04').value =='0') {this.value=''; if (this.className == 's4-searchbox-QueryPrompt') this.className = ''; else this.className = this.className.replace(' s4-searchbox-QueryPrompt',''); document.getElementById('ctl00_PlaceHolderSearchArea_SearchBox_ctl04').value=1;}" onblur="if (this.value =='') {this.value='Enter Search Term'; if (this.className.indexOf('s4-searchbox-QueryPrompt') == -1) this.className += this.className?' s4-searchbox-QueryPrompt':'s4-searchbox-QueryPrompt'; document.getElementById('ctl00_PlaceHolderSearchArea_SearchBox_ctl04').value = '0'} else {document.getElementById('ctl00_PlaceHolderSearchArea_SearchBox_ctl04').value='1';}" style="width:170px;" />

有人可以建议一个正则表达式来实现这一点

4

2 回答 2

0

试试这个——但我不太相信它是完美的。

/(?:id=('|")[^\1]*PlaceHolderSearchArea[^\1]*\1[^>]*)value=('|")(.*?\2)/

唯一的事情是它假设 ID 属性在值之前。

在这个例子中工作(JS)

'<input name="ctl00$PlaceHolderSearchArea$SearchBox$S633C1122_InputKeydummywords" type="text" value="something something" maxlength="200" id="ctl00_PlaceHolderSearchArea_SearchBox_S622C1022_InputKeywords" accesskey="S" value="hello" title="something..." class="ms-sbplain" alt="something..." style="width:170px;" />'.match(/(?:id=('|")[^\1]*PlaceHolderSearchArea[^\1]*\1[^>]*)value=('|")(.*?\2)/);
于 2012-06-14T11:56:57.843 回答
0
@"(<input)([^>]*)(type=\"")(text)(\"")([^>]*)(value=\"")([^\""]*)(\"")([^>]*)(id=\"")(\w*PlaceHolderSearchArea\w*)(\"")([^>]*)(/>)"

上面的表达式对我有用

于 2012-06-14T11:57:15.983 回答