How to determine if one string starts with what another ends with

前端 未结 3 1243
一生所求
一生所求 2021-01-29 12:50

I have a list of phrases:

[
  \"according to all known laws of aviation\",
  \"time flies when you\'re having fun...\"
  ...
]

I would like to

3条回答
  •  孤街浪徒
    2021-01-29 13:00

    If you JUST want to match the start of some array of strings, you can use a filter and a startsWith function pair.

      let test = "a ";
      const items = phrases.filter(phrase => phrase.startsWith(test));
    

    NOTE IF you do not care and as your strings are all lower case use .toLowerCase() to make it match "I" and "i";

      let test = "a ";
      const items = phrases.filter(phrase => phrase.toLowerCase().startsWith(test));
    

    Note: I edited this live example to show "contains" as well via a checkbox, just in case that helps illustrate an alternative.

    Here is a live example:

    let phrases = ["according to all known laws of aviation", "a blessing in disguise", "a bunch", "a dime a dozen", "beat around the bush", "better late than never", "bite the bullet", "break a leg", "call it a day", "cut somebody some slack", "cutting corners", "dead tired", "easy does it", "excuse me", "get it out of my system", "get it out of your system", "get out of hand", "get something out of my system", "get something out of your system", "get your act together", "give someone the benefit of the doubt", "go back to the drawing board", "good idea", "hang in there", "hit the nail on the head", "hit the sack", "how does that sound?", "i don't know", "i don't understand", "i love you", "i owe you one", "i'm fine", "i'm fine, thanks", "i'm really sorry", "i'm sorry", "it cost an arm and a leg", "it costs an arm and a leg", "it'll cost an arm and a leg", "it will cost an arm and a leg", "it's not rocket science", "i've never given it much thought", "kill two birds with one stone", "killing two birds with one stone", "let you off the hook", "let me off the hook", "look, it's", "make a long story short", "miss the boat", "never gonna give you up", "no pain,  no gain ", "on the ball ", "once upon a time ", "pull your leg ", "pulling your leg ", "pull yourself together ", "proof of concept ", "so far so good ", "so much ", "speak of the devil ", "thanks so much ", "thank you so much ", "that's the last straw ", "the best of both worlds ", "this is a test", "time flies when you're having fun", "to be honest", "to get bent out of shape", "to kill a mockingbird", "to make matters worse", "under the weather", "watch out", "we'll cross that bridge when we come to it ", "whatever floats your boat", "whatever you say ", "wrap your head around something", "you can say that again", "your guess is as good as mine", "there's no such thing as a free lunch", "throw caution to the wind", "you can't have your cake and eat it too ", "have your cake and eat it too", "judge a book by its cover ", "book by its cover", "last straw", "shut up", "be quiet", "how are you?", "never gonna give you up", "water under the bridge", "let you down", "birds and the bees", "pair of trainers", "i'd really like", "i wouldn't mind", "i could do with"];
    
    $('#mywords').on('input change', function(event) {
      let grp = $("
    "); let test = $(this).val(); $('#testing').html(test); const items = phrases.filter(phrase => phrase.startsWith(test)); $.each(items, function(index, value) { let rowItem = $("
    ").text(index + ". " + value); grp.append(rowItem); }); $('#results-list').html(grp.children()); }); function dochange(event) { let grp = $("
    "); let test = $('#mywords').val(); $('#testing').html(test); let items = []; if (!$('#mywords-contain').prop('checked')) { items = phrases.filter(phrase => phrase.toLowerCase().startsWith(test)); } else { items = phrases.filter(phrase => phrase.toLowerCase().includes(test)); } $.each(items, function(index, value) { let rowItem = $("
    ").text(index + ". " + value); grp.append(rowItem); }); $('#results-list').html(grp.children()); } $('#mywords-contain').on('change', dochange); $('#mywords').on('input change', dochange);
    
    (empty)
    
    Result found:

提交回复
热议问题