Jump to content

Applying RegExp to an object prototype


musicman

Recommended Posts

/* Merry Christmas everyone! */ :dirol:

I have an array of objects:

arr = [ obj1, obj2 ];

Each object has prototypes:

obj1.txt = "az";
obj2.txt = "abc";
obj1.isRoot = false;
obj2.isRoot = false;

And there's an array called dictionary, it formed like this:

var dictionary = 
[
    ["a","(n)"],
    ["ab","(n)"],
    ["abc","(n)"],
    ["abcd","(n)"]
]

What I try to achieve is when obj.txt is similar to dictionary[x][0], then obj.isRoot turn to TRUE.
I've made the function below so far but it doesn't work.

function go(){
   
   for ( i=0; i<arr.length; i++ ){
       
        txt_source = arr[i].txt.replace( /\~|\`|\!|\@|\#|\$|\%|\^|\&|\*|\(|\)|\_|\-|\+|\=|\{|\}|\[|\]|\:|\;|\"|\'|<|\>|\,|\.|\?|\/|\\|(0-9)/g, "" );
        
        ori = txt_source.toLowerCase();
        
        regex = new RegExp( '\\b' + ori, 'gi');
       
        for ( m=0; m<dictionary.length; m++ ){
           
            if ( dictionary[m][0].match(regex) ){
               
                arr[i].isRoot = true; // I want this happen for obj2.txt = "abc".
           
            } else {
               
                arr[i].isRoot = false;
               
            }
           
        }
       
    }
   
    console.log( ahoc_arr[x].isRoot ); // all objects always FALSE
   
}

Please help!

Thanks!

Edited by musicman
Link to comment
Share on other sites

If the regular expression matches, then you need to stop looping. Everything is false because you always loop through every item in the dictionary, so isRoot is going to be set to whatever the last match was, and neither of them match abcd. If it matches and you set isRoot to true then stop looping at that point. You'll also find that both objects will match.

Link to comment
Share on other sites

Thanks @justsomeguy and Happy New Year!

 

now I've found a little bit strange fact that the code above is actually works! :umnik2:

 

However, in my original javascript file, the 'dictionary' has more than 20k items.

It won't work if the item of the 'dictionary' is more than 20 items.. ???

how is that happen? is it the browser or my pc spec maybe?

Link to comment
Share on other sites

If there's an error happening, you'll see the message in the console.

 

Otherwise, consider what you're asking it to do. If you have 20k items in your dictionary, and let's say 10 objects in that array that you're searching through, then you're asking it to do up to 200,000 regular expression matches. It's going to take some time to finish that, depending on how fast your machine is. Regular expression matching can be an expensive operation.

Link to comment
Share on other sites

No. no error, console just tell the result that it is false and you're right, this method is not efficient.

But I found another way and it works well.

 

I create a regexp obj like this:

 

var regex = /\bab\b|\babc\b|\babcd\b/gi;

 

regex has more than 20k items, this replacing the dictionary array on the above code.

And then, to match the obj.txt with dictionary items, I'm looping a conditional function like this:

 

if ( arr.txt.match( regex ) ){

arr.isRoot = true;

} else {

arr.isRoot = false;

}

 

console.log( arr[1].isRoot ); // finally true

 

It works now. Maybe the problem was something to do with the recursion on previous code but I'm not sure.

 

Thanks @justsomeguy!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...