Regex: Matching the first letter(s) of multiple words

So I am really bad at regex stuff ... I googled for an hour now and the best answer I could find is this one.

But I still can't for the sake of me figure it out ...

What I need is the following:

I have a JS array I need to .filter() . This arrays contains for example:

[ 'Gurken halbiert 2kg', 'Karotten geschnitten 5kg', 'Gurken RW' ]

For example: All of the following inputs should match the first entry ( "Gurken halbiert 2kg" ):

  • "Gu ha"
  • "gur h"
  • "gu halbi"
  • The following inputs should not match it:

  • "ken halbi"
  • "urk ha"
  • "gur biert"
  • Why? because one or more letters at the beginning of any word are missing.

    More example inputs and the entries they should match:

  • input: "Gur" -> matches 1st and 3rd entry
  • input: "Kar g 5" -> matches 2nd
  • input: "G r" -> matches 3rd
  • I really hope someone can help as I am totally lost in this RegEx chaos - I never really understood them or how to use them.


    Since the input varies, you would need to dynamically generate the regular expression.

    In the function below, you will notice that we are basically building a string and then creating the regular expression using new RegExp(string, 'i') .

    The expression starts with a caret, and then basically follows the pattern:

    ^[[nth input char(s)]]w*s+[[nth input char(s)]]w*s+[[nth input char(s)]]w*
    

    It's worth pointing out that w* is added after each input string and s+ is added if it's not the last input string (ie, not the end).

    function generateRegex (input) {
      var string = '^', arr = input.trim().split(' ');
      arr.forEach(function (chars, i) {
        string += chars + 'w*' + (arr.length - 1 > i ? 's+' : '');
      });
    
      return new RegExp(string, 'i');
    }
    

    Then you can use the .filter() method on your array and return the elements that match:

    var array = ['Gurken halbiert 2kg', 'Karotten geschnitten 5kg', 'Gurken RW'];
    var filteredArray = array.filter(function (value) {
      return value.match(generateRegex('Gur Ha'));
    });
    

    Output:

    'Gur Ha' would match: ["Gurken halbiert 2kg"]

    'Gur' would match: ["Gurken halbiert 2kg", "Gurken RW"]

    'Kar g 5' would match: ["Karotten geschnitten 5kg"]

    'G r' would match: ["Gurken RW"]


    Example:

    function generateRegex (input) {
      var string = '^', arr = input.trim().split(' ');
      arr.forEach(function (chars, i) {
        string += chars + 'w*' + (arr.length - 1 > i ? 's+' : '');
      });
    
      return new RegExp(string, 'i');
    }
    
    var array = ['Gurken halbiert 2kg', 'Karotten geschnitten 5kg', 'Gurken RW'];
    var filteredArray = array.filter(function (value) {
      return value.match(generateRegex('Gur'));
    });
    
    document.body.textContent = JSON.stringify(filteredArray);

    Here is an example of how to filter user input as specified.

    Notes

  • Escaping of regular expression characters from user input pattern (always sanitise user input).
  • Explicitly not using " w " in order to support characters like " é ".
  • Supports white space characters other than <space> like <tab> which can be copied and pasted into user input fields causing the user to think it's broken.
  • function doSubmit() {
      // Get the user input search pattern string 
      var userInput = document.getElementById("myinput").value,
    
        // List of strings to search
        testList = [
          'Gurken halbiert 2kg',
          'Karotten geschnitten 5kg',
          'Gurken RW'
        ],
    
    
    
        // Between our "words" we allow zero or more non-space characters "[^s]*"
        //  (this eats any extra characters the user might not have specified in their search pattern)
        // followed by one or more white-space characters "s+"
        //  (eating that space between the "words")
        // Note that we are escaping the "" characters here.
        // Note we also don't use "w" as this doesn't allow for characters like "é".
        regexBetween = '[^s]*s+',
    
        // Match the start of the string "^"
        // Optionally allow one or more "words" at the start
        //  (this eats a "word" followed by a space zero or more times).
        // Using an empty string here would allow "o g" to match the 2nd item in our test array.
        regexStart = '^(?:' + regexBetween + ')*',
    
        // Clean whitespace at begining and end
        regexString = userInput.trim()
    
          // Escape any characters that might break a regular expression
          // Taken from: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions
          .replace(/[.*+?^${}()|[]]/g, "$&")
    
          // Split into array of "words"
          .split(/s+/)
    
          // Combine the "words" building our regular expression string
          .join(regexBetween),
    
        // Create the regular expression from the string (non-case-sensitive 'i')
        regexObject = new RegExp(regexStart + regexString, 'i'),
    
        // Filter the input array testing for matches against the regular expression.
        resultsList = testList.filter(function(item) {
          return regexObject.test(item);
        });
    
      // Ouput the array into the results text area, one per line.
      document.getElementById('output').value = resultsList.join('n') + 'n===the end===';
    }
    <form id="myform" onsubmit="doSubmit(); return false;">
      <input type="text" id="myinput" value="" />
      <input type="submit" name="submit" />
    </form>
    <textarea id="output" rows="5" cols="30">
    </textarea>
    链接地址: http://www.djcxy.com/p/77084.html

    上一篇: stdout = subprocess.PIPE和stdout = PIPE之间的区别

    下一篇: 正则表达式:匹配多个单词的第一个字母