regular expression for multiple search in list seperated by comma

powercoder23

I am having a list similar to this which works with the simple regular expression, now my requirement is to add a comma separated multiple search option.

For example in this right now if i am typing "Elaine" it shows me "Elaine Marley", now i want, if i am typing "Elaine, Stan" it should return me two result "Elaine Marley" & "Stan".

Please let me know in case more details needed, any help would be appreciated.

Can anyone help me with the regular expression?

Thanks

Dhiraj

leaf

Take a look at the the demo before reading :

// http://stackoverflow.com/a/3561711/1636522

RegExp.escape = function(s) {
  return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
};

// vars

var span = getEl('span'),
  input = getEl('input'),
  li = getEls('li'),
  tid;
  
// onkeyup

addEventSimple(input, 'keyup', function(e) {
  // cancels previous query
  tid && clearTimeout(tid);
  // waits 250ms then filters
  tid = setTimeout(function() {
    tid = null;
    span.textContent = +span.textContent + 1;
    filter(e.target.value);
  }, 250);
});

// filtering

function filter(input) {
  var i = 0,
    l = li.length,
    re = input && toRegex(input),
    el;
  for (; i < l; i++) {
    el = li[i]; // list item
    if (!re || re.test(el.textContent)) {
      el.style.display = 'list-item';
    } else {
      el.style.display = 'none';
    }
  }
}

// input > regex

function toRegex(input) {
  input = RegExp.escape(input);
  input = input.match(/[^,\s]+(\s+[^,\s]+)*/g) || [];
  input = input.join('|');
  return new RegExp(input, 'i');
}

// http://www.quirksmode.org/js/eventSimple.html

function addEventSimple(obj, evt, fn) {
  if (obj.addEventListener) obj.addEventListener(evt, fn, false);
  else if (obj.attachEvent) obj.attachEvent('on' + evt, fn);
}

// helpers

function getEl(tag) {
  return getEls(tag)[0];
}

function getEls(tag) {
  return document.getElementsByTagName(tag);
}
<input type="text" placeholder="Example : &quot;nn, oo, ca&quot;." />
<div style="padding:.5em .5em 0">Filtered <span>0</span> times.</div>
<ul>
  <li>Guybrush Threepwood</li>
  <li>Elaine Marley</li>
  <li>LeChuck</li>
  <li>Stan</li>
  <li>Voodoo Lady</li>
  <li>Herman Toothrot</li>
  <li>Meathook</li>
  <li>Carla</li>
  <li>Otis</li>
  <li>Rapp Scallion</li>
  <li>Rum Rogers Sr.</li>
  <li>Men of Low Moral Fiber</li>
  <li>Murray</li>
  <li>Cannibals</li>
</ul>

Here I'll only expose the toRegex function. Say that we have entered the following value : "el, le, az".

var regex = toRegexp('el, le, az'); // regex = /el|le|az/i
regex.test('Elaine'); // true  -> show
regex.test('Marley'); // true  -> show
regex.test('Stan');   // false -> hide

The resulting regular expression (/el|le|az/i) means : search for "el" or "le" or "az", and ignore the case (allows "EL", "Le" or "aZ" as well). Now, let's read this function line by line :

input = RegExp.escape(input); // http://stackoverflow.com/q/3561493/1636522
input = input.match(/[^,\s]+(\s+[^,\s]+)*/g) || []; // ["el", "le", "az"]
input = input.join('|'); // "el|le|az"
return new RegExp(input, 'i'); // /el|le|az/i

Let's go further about /[^,\s]+(\s+[^,\s]+)*/g :

[^,\s]+         any char except comma and whitespace, one or more times
(\s+[^,\s]+)*   one or more whitespaces + same as above, zero or more times
g               grab all occurrences

Usage example with a stupid input :

'a,aa,aa a, b , bb , bb b , , '.match(/[^,\s]+(\s+[^,\s]+)*/g);
// ["a", "aa", "aa a", "b", "bb", "bb b"]

That's it ! Hope that was clear enough :-)

Further reading : http://www.javascriptkit.com/javatutors/redev.shtml.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Regular expression for three word seperated by two comma

From Dev

Regex expression - one or multiple strings seperated by comma

From Dev

Creating multiple empty list seperated by comma

From Dev

RegEx for comma seperated list

From Dev

Java multiple regular expression search

From Dev

Regular Expression for last value in comma separated list

From Dev

Comma Seperated Query for Multiple Parametes in PLSQL

From Dev

Insert Comma seperated varchar into multiple column value?

From Dev

Comma Seperated Query for Multiple Parametes in PLSQL

From Dev

regular expression for a pattern of 8 characters seperated by a hypen

From Dev

Removing value from a comma seperated list with Regex

From Dev

Regex to allow a comma seperated list of codes

From Dev

To get records in comma seperated List LINQ to Entities

From Dev

Creating a Comma Seperated List from a subquery in SQL

From Dev

Record the types of a comma seperated list of identifiers?

From Dev

How to display comma seperated list into table in EmberJS?

From Dev

Regular expression to search multiple strings (Textpad)

From Dev

Regular expression in PHP with comma

From Dev

Regular Expression - difficulties with comma

From Dev

Looking for a regular expression to work on a list of comma separated values

From Dev

How to insert a variable with two comma seperated values into a variable containing multiple comma seperated values in Bash

From Dev

Making a regex expression for adding single quotes in numbers comma seperated

From Dev

Parse multiple choice list with Regular Expression

From Dev

Parse multiple choice list with Regular Expression

From Dev

Regular expression for a comma separated string

From Dev

Regular expression to validate chars by comma

From Dev

Regular expression for a comma separated string

From Dev

Comma seperated steps with one value to multiple steps with the same value

From Dev

Excel get multiple cell value as comma seperated array

Related Related

  1. 1

    Regular expression for three word seperated by two comma

  2. 2

    Regex expression - one or multiple strings seperated by comma

  3. 3

    Creating multiple empty list seperated by comma

  4. 4

    RegEx for comma seperated list

  5. 5

    Java multiple regular expression search

  6. 6

    Regular Expression for last value in comma separated list

  7. 7

    Comma Seperated Query for Multiple Parametes in PLSQL

  8. 8

    Insert Comma seperated varchar into multiple column value?

  9. 9

    Comma Seperated Query for Multiple Parametes in PLSQL

  10. 10

    regular expression for a pattern of 8 characters seperated by a hypen

  11. 11

    Removing value from a comma seperated list with Regex

  12. 12

    Regex to allow a comma seperated list of codes

  13. 13

    To get records in comma seperated List LINQ to Entities

  14. 14

    Creating a Comma Seperated List from a subquery in SQL

  15. 15

    Record the types of a comma seperated list of identifiers?

  16. 16

    How to display comma seperated list into table in EmberJS?

  17. 17

    Regular expression to search multiple strings (Textpad)

  18. 18

    Regular expression in PHP with comma

  19. 19

    Regular Expression - difficulties with comma

  20. 20

    Looking for a regular expression to work on a list of comma separated values

  21. 21

    How to insert a variable with two comma seperated values into a variable containing multiple comma seperated values in Bash

  22. 22

    Making a regex expression for adding single quotes in numbers comma seperated

  23. 23

    Parse multiple choice list with Regular Expression

  24. 24

    Parse multiple choice list with Regular Expression

  25. 25

    Regular expression for a comma separated string

  26. 26

    Regular expression to validate chars by comma

  27. 27

    Regular expression for a comma separated string

  28. 28

    Comma seperated steps with one value to multiple steps with the same value

  29. 29

    Excel get multiple cell value as comma seperated array

HotTag

Archive