0

I have seen questions and answers for using variables with RegEx like this:

var re = new RegExp(yyy, 'g');
xxx.match(re);

How do I add the caret and the dollar sign for an exact match?

I am iterating through an array: [-10, 0, 10] as search values. So, I am trying to do something like this:

    var re = new RegExp("^" + data[i] + "$", 'g');

If I am searching an array of [-10, 0, 10] and if I search for 0, I get all three. I need to search for only 0 with this: /^0$/


Here is my entire code with test cases. I am trying to find unique items using RegEx / match.

function nonUniqueElements(data){
    var arrForDeletion = [];
    var strData = data.join(",");

    for(var i = 0; i < data.length; i++){
        var re = new RegExp(data[i], 'g');
        console.log("re: " + re);
        var matches = strData.match(re);

        if(matches.length === 1){
            arrForDeletion.push(data.indexOf(data[i]));
        }
    }

    if(arrForDeletion.length > 0){ // If there are unique items in the input array, do not include them in the returned array.
        var arrReturn = [];     

        data.forEach(function(item,idx){
            if(arrForDeletion.indexOf(idx) === -1){
                arrReturn.push(item);
            }           
        });
        console.log("arr to be returned:");
        console.log(arrReturn);
        return arrReturn;
    }
    else{ // There were no unique items in the input array.
        console.log("no unique items:");
        console.log(data);
        return data;
    }

}

/*
nonUniqueElements([1, 2, 3, 1, 3]); // == [1, 3, 1, 3]

nonUniqueElements([1, 2, 3, 4, 5]); // == []

nonUniqueElements([5, 5, 5, 5, 5]); // == [5, 5, 5, 5, 5]

nonUniqueElements([10, 9, 10, 10, 9, 8]); // == [10, 9, 10, 10, 9]
*/
nonUniqueElements([-10,10,0]); // []
9
  • 2
    OK? What's the problem? /^0$/ will match the string "0" and only that string. Commented Aug 8, 2018 at 15:08
  • How do I use that with the variable in the example using the constructor? Commented Aug 8, 2018 at 15:10
  • @phrazzled you pasted some code. Doesn't it work ? Commented Aug 8, 2018 at 15:10
  • I need to add the caret and dollar sign to the data[i] Commented Aug 8, 2018 at 15:10
  • 2
    @phrazzled You did with "^" + data[i] + "$". Unless you mean that you want to include "^" and "$" in your match. Maybe you should describe what problem you're trying to solve and we can help more directly. Commented Aug 8, 2018 at 15:11

2 Answers 2

2

You're getting all 3 because for each entry in your array you are searching for exactly that value. Which will of course return all 3.

If you only want to match values of exactly 0 you can simply do:

const regex = new RegExp('^0$'); // Only need to compile it once

for(let i = 0; i < arr.length; i++){
  arr[i].match(re);
}

Or if you are only checking for 0 values why not just do arr[i] === 0 or arr[i] === '0' depending on if your input is a string or a number.

Sign up to request clarification or add additional context in comments.

2 Comments

I cannot use: const regex = new RegExp('^0$'). The code needs to adapt for each value in the array to see if each value is unique.
In that case you're better off just storing a list of elements which you have already seen in an object instead of trying to use regex. Because you are checking for a direct match on the element itself which is always going to be true.
0

Oh, wait...Geez...

I am searching the entire string of the joined array:

"-10,10,0"

So, of course it will find 0 three times.

Geez... (See what lack of sleep will result in?)

I think my whole approach to this project was wrong. But, I wanted to use RegEx and match.

I apologize.

Thanks for looking at this. Sorry. I am unable to delete it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.