title: Regular Expression Javascript with XCode
body: <p>i am trying to sieve out the username and text fields of a HTML Form loading in a web view. Previously i tried this method to narrow down the corresponding text fields that i need but it was tedious and there is no end to the computation of variables. even though i thought i listed out a lot of variables, it was still a hit and miss solution, not all website works.</p>
  
  <pre><code>var inputFields = document.querySelectorAll("input[type='text']"); 
  for (var i = inputFields.length &gt;&gt;&gt; 0; i--;) { 
          if ( (inputFields[i].getAttribute('name') == 'email')
          ||   (inputFields[i].getAttribute('name') == 'Email')
          ||   (inputFields[i].getAttribute('name') == 'e-mail')
          ||   (inputFields[i].getAttribute('name') == 'E-mail')
          ||   (inputFields[i].getAttribute('name') == 'emailerr')
          ||   (inputFields[i].getAttribute('name') == 'UID')
          ||   (inputFields[i].getAttribute('name') == 'uid')
          ||   (inputFields[i].getAttribute('name') == 'username')
          ||   (inputFields[i].getAttribute('name') == 'Username')
          ||   (inputFields[i].getAttribute('name') == 'userName')
          ||   (inputFields[i].getAttribute('name') == 'user_name')
          ||   (inputFields[i].getAttribute('name') == 'User_Name')
          ||   (inputFields[i].getAttribute('name') == 'User_name')
          ||   (inputFields[i].getAttribute('name') == 'userid')
          ||   (inputFields[i].getAttribute('name') == 'userID')
          ||   (inputFields[i].getAttribute('name') == 'UserID')
          ||   (inputFields[i].getAttribute('name') == 'Userid')
          ||   (inputFields[i].getAttribute('name') == 'ID')
          ||   (inputFields[i].getAttribute('name') == 'id')
          ||   (inputFields[i].getAttribute('name') == 'j_username')
          ||   (inputFields[i].getAttribute('name') == 'session_key')
          ||   (inputFields[i].getAttribute('name') == 'login_password')) {
                               inputFields[i].value = '%@';}}
  </code></pre>
  
  <p>Then i was told about RegularExpression but i wasn't very familiar with it and i read some tutorials about it but not specific to my project to try and get what it means and i piece out this code which is also a hit and miss solution. I am not even sure if i am doing it right but the code is definitely short this time for sure. </p>
  
  <pre><code>var inputFields = document.querySelectorAll("input[type='text']"); 
  for (var i = inputFields.length &gt;&gt;&gt; 0; i--;) { 
       regStr = /(mail|user|iden|name|id|key|login|username|email)/i; 
       searchName = inputFields[i].getAttribute('name').search(regStr);
       searchId = inputFields[i].getAttribute('id').search(regStr);
       if (!(searchName == -1) || !(searchId == -1)){ 
            inputFields[i].value = '%@';}}
  </code></pre>
  
  <p>Can someone tell me if i have done this RegularExpression correctly? i really have no idea. Some sites it works, some it doesn't. I understand that what i am searching for is configurable by developers and i won't have a 100% hit on all site, but what puzzle me is that sometime even though the term matches exactly one of my search variable it didn't work.</p>