0

I've ask how to handle inconsistent HTML tag attribute position here: Regex get html input value dynamic tag position. I want to ask another question how to match only specific that the string separate by SPACE, not by break line. This is the string example:

<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="joko145" name="confirmedUsername" id="username" maxlength="60" class="txt-box  txtbox-m "  aria-describedby="" aria-required="true" disabled="disabled">

I've tried this:

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

to get the value, but instead it return the username value, it return emailaddress value. The first return array (full text detected by regex) like this:

<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="joko145" name="confirmedUsername" id="username" maxlength="60" class="txt-box  txtbox-m "  aria-describedby="" aria-required="true" disabled="disabled">

It detect all the from the string, not specific that I want like below:

<input type="text" autocomplete="" value="joko145" name="confirmedUsername" id="username" maxlength="60" class="txt-box  txtbox-m "  aria-describedby="" aria-required="true" disabled="disabled">

It happened only when HTML tag separate by SPACE, not by break line. How to get only the username when it separate by SPACE? Thanks.

  • What about https://regex101.com/r/d3ueaN/1? – revo Mar 04 '18 at 16:47
  • That's the correct answer if you're using php. When I change the mode to javascript "Group1" which is the value, did not available. I also have tried it in my local apps, the value did not available. Any other solution, @revo? – Christoforus Surjoputro Mar 05 '18 at 04:03
  • It works in JS too. You can have access to first capturing group by `$1` https://regexr.com/3lm6n – revo Mar 05 '18 at 05:09
  • I think it is not a good solution, because it is non standard. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/n. Any other suggestion @revo? – Christoforus Surjoputro Mar 06 '18 at 07:39
  • Indeed, that reference is about the `$n` properties of the `RegExp` object, not the `$n` back-references. The latter is used without reservation in the [Regular Expressions topic on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions). You may get value the same way you did in your other question. There is no difference. – revo Mar 06 '18 at 15:35

0 Answers0