Javascript regex with XCode

I am trying to cross out username and HTML form load text boxes in a web view. I have tried this method before to narrow down the appropriate text fields that I need, but it was tedious and there is no end to evaluating variables. even though I thought I was listing a lot of variables, this was still a problem and error solution and not the whole website.

var inputFields = document.querySelectorAll("input[type='text']"); 
for (var i = inputFields.length >>> 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 = '%@';}}

      

Then I was told about RegularExpression but I was not very familiar with it and I read several tutorials about it but was not defined for my project to try and understand what it means and I am posting this code which is also a hit and skip the solution. I'm not even sure if I'm doing it right, but this time the code will most likely be short.

var inputFields = document.querySelectorAll("input[type='text']"); 
for (var i = inputFields.length >>> 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 = '%@';}}

      

Can anyone tell me if I did this RegularExpression correctly? I really have no idea. Some sites work, some don't. I understand that what I'm looking for is customizable by the developers and I won't get 100% hit on the whole site, but what puzzles me is this someday, although this term matches one of my search variables , it didn't work.

+3


source to share


1 answer


This is SPARTA!

Your RegExp doesn't match the list of values ​​given in your looong code.



var opts = "email,e-mail,emailerr,uid,username,userid,id,j_username," +
    "session_key,login_password,live,liveid,live_id,live_email";
var re = new RegExp(opts.replace(/,/g, "|"), "i");
if (re.test(inputFields[i].getAttribute('name'))) {
    inputFields[i].value = '%@';
}

      

PS: You're not going to use it as some kind of spam utility, are you?

0


source







All Articles