Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

167
Views
search text in array using regex in javascript

I am very new to making search text in array some elements in array are in rangers i.e it cant be anything after certain text in this AA and A regex and I have multi-dimensional array and I want search text in each array . So I wrote something like this.I put AA* in array so only first 2 character should match and A* for only one character match.

arr = [
     ["AA*","ABC","XYZ"] ,
    ["A*","AXY","AAJ"]
    ]
    var text = "AA3";
    for ($i=0; $i<arr.length; $i++ ){
        var new_array = [];
        new_array = arr[$i];
        new_array.filter(function(array_element) {
              var result = new RegExp(array_element).test(text);
              
              if( result == true){
                console.log(arr[$i]);
          } 
            
          });
    }

So what i want is when text = "AA3" or anything after double A AA[anything] and the output should be first array which is ["AA*","ABC","XYZ"] but I am getting both array as output and when text = "A3" then output should be second array which is ["A*","POI","LKJ"] but I am getting both array.But if text = "ABC" or text = "AAJ" then it should output first array or second array respectively.I dont know anything about how to write regex or is there anyway I can implement this using any other method. Thanks in advance any advice will be helpful.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Summary

In short, the issue is "*"! The * found in the members of the set array is why you're getting the same array each time.

Detailed Info

Regexp is a one concept most developers find hard to understand (I am one of such btw 😅).

I'll start off with an excerpt intro to Regexp on MDN

Regexp are patterns used to match character combinations in strings - MDN

With that in mind you want to understand what goes on with your code. When you create a Regex like /A*/ to test "AA3", what would be matched would be A, AA, etc. This is a truthy in javascript. You would want a more stricter matching with ^ or $ or strictly matching a number with \d.

I rewrote your code as a function below:

arr = [
    ["AA*", "ABC", "XYZ"],
    ["A*", "AXY", "AAJ"],
];

findInArray(arr, "AA3") // prints both array
findInArray(arr, "AAJ") // prints second array
findInArray(arr, "ABC") // prints first array

function findInArray(array, value) {
    return array.filter((subArray) =>
        subArray.some((item) => {
            const check = new RegExp(value);
            return check.test(item);
        })
    );
}
about 4 years ago · Juan Pablo Isaza Report

0

Problem

The problem lies in the fact you use each of the strings as a regex.

  • For a string with a * wildcard, this evaluates to zero or more matches of the immediately preceding item, which will always be true.
  • For a string consisting solely of alphanumerics, this is comparing a string to itself, which similarly will always give true.
  • For strings containing characters that constitute the regex's syntax definition, this could result in errors or unintended behavior.

MDN article on RegExp quantifiers

Rewrite

Assumptions:

  • The value with a * wildcard is always only at the 0th position,
  • There is only one such wildcard in its string,
  • The question mentions that for text = 'AAJ' only the 2nd array shall be returned, but both the AA* from the 1st array and AAJ from the 2nd would seem to match this text.
    As such, I assume the wildcard can only stand for a number (as other examples seem to suggest).

Code:

const abc = (arrs, text) => {
  return arrs.filter(arr => {
    const regex = new RegExp(`^${arr[0].replace('*', '\\d+')}$`);
    return regex.test(text) || arr.includes(text);
  })
}



const arr = [
  ["AA*", "ABC", "XYZ"],
  ["A*", "AXY", "AAJ"]
];

console.log(
`1=[${abc(arr, "AA3")}]
2=[${abc(arr, "ABC")}]
3=[${abc(arr, "AAJ")}]`);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!