Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

searching on an array when search string contains wild card characters in typescript/javascript?

I am having an array of phone numbers . I have also a search string. The search string is provided by the user. The search string can contain wild cards such as asterik(*) or question mark (?). I need a way to filter out elements from my phone number array .

 The following is my phone number array.
    let mobileNumbersArray = ["830456481", "831456481", "9886503103" ]

    **Condition 1** 
    when search string is * 
    let searchString = "*" 
    expected result = ["830456481", "831456481", "9886503103"]
    
    **Condition 2**
    when search string is 83* 
    let searchString = "83*" 
    expected result = ["830456481", "831456481"]
    
    **condition 3**
    let searchString = "*456*"
    expected result = ["830456481", "831456481"]
    
    **condition 4**
    exact search
    let searchString = "9886503103"
    expected result = ["9886503103"]
    
    **condition 5**
    exact search
    let searchString = "83?4"
    expected result = ["830456481", "831456481"]

how can i achieve searching a string on my phone number array and getting the result as an array in the cleanest way as possible.

appreciate any help

thank you .

question from:https://stackoverflow.com/questions/65649124/searching-on-an-array-when-search-string-contains-wild-card-characters-in-typesc

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think I may have an interesting solution. Please take a look at the following snippet and let me know if it works for you.

You can read about Regular Expressions on MDN.

Then about the RegExp Constructor. Here is the key. Not everyone know that you can generate Regular Expressions dynamically.

Update:

The idea is to traduce the 'wildcard' syntax into a RegExp syntax, like this:

  • ? means: 'any single number', in RegExp: [0-9] or d

  • * means: 'one or more numbers', in RegExp: .+ (actually this means one or more 'anything')

This is why I do the double replacement at the beginning before feeding the query to the RegExp constructor.

Finally, yes, this can be extended to work with non number characters. It is sufficient to replace [0-9] with .

function queryNumbers(array, query) {
  const src = query.replace(/?/g, '[0-9]').replace(/*/g, '.+');
  const regexp = new RegExp('^' + src + '$');
  return array.filter((item) => regexp.test(item));
}

const mobileNumbersArray = ['830456481', '831456481', '9886503103'];

console.log(queryNumbers(mobileNumbersArray, '*'));
console.log(queryNumbers(mobileNumbersArray, '83*'));
console.log(queryNumbers(mobileNumbersArray, '*456*'));
console.log(queryNumbers(mobileNumbersArray, '9886503103'));
console.log(queryNumbers(mobileNumbersArray, '83?4*'));
console.log(queryNumbers(mobileNumbersArray, '8*'));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...