/    Sign up×
Community /Pin to ProfileBookmark

Return multi match string

I have the following code which returns an object if the searchText matches a surname or firstname of an object in a object list.

This works

If I enter “”Donald Duck” I get all the Donald’s, no Ducks are returned and it’s not filtered to just Donald Ducks.

What I should get is when I enter Donald. I get all the Donalds with their lastnames, If add a “e” on the end I get no results as it’s spelt wrong. Once I type Duck I only get first and last name matches.

Also need it to work for Duck Donald

Any ideas?

[code]
let matches = studentList.filter(student=>{
const regex = new RegExp(`^${searchText}`, ‘gi’);
return student.Surname.match(regex) || student.Firstname.match(regex);
});
[/code]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERMay 22.2021 — What does your comparison list look like other than the single 'Donald Duckl'?

Are all 2 word name entries (first, last)?

To which word would you add an 'e'? Donalde? Ducke? or both?

Do you have some additional sample code with which to test against (ie, HTML, etc)?
Copy linkTweet thisAlerts:
@kiwisauthorMay 22.2021 — @JMRKER#1632008

What do you mean, what does my first comparison look like?

I have a list of objects, all have multiple properties. first and last names are two, all should in theory have them.

A front end user is searching for one, so might search by the lastname or first name first.

BUT once they're spelt it incorrectly (e on Donald or Duck) it shouldn't return. Likewise once they're onto the second word, D in Duck for example. Donald Smith shouldn't show.

Available results show in the format of Lastname/Firstname sorted alphabetically. So user behavior may be to include the / so I likely need to manage that too,
Copy linkTweet thisAlerts:
@kiwisauthorMay 22.2021 — I add an event listener onto a text inputbox, when a user inputs a value it calls a function.

That function loops through a JSON array data to find matches and then returns each match so it can be added as a DIV with data to a parent DIV back on the main page as options so a user can collect one

What I'm thinking of doing is splitting the input into an array based on space " " or /

The match each array item against first and last name options in my object list.

The trick is, once the first item is matched, the second must be a filtered array of those options. For example...

Donald Mouse

Donald Horse

Donald Duck

Donald Monkey

Donald Goat

I'm really not sure how to explode the user input based on space or slash or do subsequent matches
Copy linkTweet thisAlerts:
@kiwisauthorMay 22.2021 — This is where I'm at.

I'm splitting my search text into an array. So I can check each word at a time.

I'm not sure how to do the following

  • 1. Add each match into my options array.

  • 2. How to remove anything where Donalde for example is typed. I believe it returns true because Donald is in Donalde

  • 3. When looping over second and subsequent words, how to remove invalid students from my options list?

  • 4. As above as I type Do for Donald, Dominque will return true so will need to be removed after the ''M'.


  • I'm wondering if this logic and approach is completely wrong.

    <i>
    </i> /* studentList JSON data returned from fetch, each student has surname and firstname as properties */
    var str = "Donald";
    var res = str.split(/[s.*+-/_]/);
    console.log(res);

    <i> </i> var options = [];
    <i> </i>
    <i> </i> res.forEach(myWord);

    <i> </i> function myWord(item, index){
    <i> </i> let matches = studentList.filter(student=&gt;{
    <i> </i> const regex = new RegExp(<span><code>${item}</code></span>, 'gi');
    <i> </i> options = student.Surname.match(regex);
    <i> </i>
    <i> </i> });
    <i> </i> }

    <i> </i> console.log(options);
    Copy linkTweet thisAlerts:
    @SempervivumMay 23.2021 — >How to remove anything where Donalde for example is typed. I believe it returns true because Donald is in Donalde

    This will check for a precise match, e. g. Donalde will not match:

    `const regex = new RegExp('^' + item + '$', 'gi');`

    (not tested)
    Copy linkTweet thisAlerts:
    @kiwisauthorMay 23.2021 — This is what I have now, inside my function I create a copy of the studentList

    <i>
    </i>matches = studentList;


    Then split the input as above and forEach each word with a function using this code

    <i>
    </i>function myWord(item, index){
    matches = matches.filter(student=&gt;{ <br/>
    const regex = new RegExp(<span><code>${item}</code></span>, 'gi');
    return student.Surname.match(regex) || student.Firstname.match(regex);
    });
    }


    I think this is working
    ×

    Success!

    Help @kiwis spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.27,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...