I’ve written a live filter in javascript that takes a value from a field and hides the rows in a table that do not match.
The RegEx I use for this is very simple: /inputValue/i
Although this works great it only matches characters that are in order. For example:
inputValue = test
string to match = this is a test sentence
This example would match, but if I tried:
inputValue = this sentence
string to match = this is a test sentence
This won’t match because the input value is out of order.
How would I go about writing a RegEx that is in order but can skip words?
Here is the loop I currently use:
for (var i=0; i < liveFilterDataArray.length; i++) {
var comparisonString = liveFilterDataArray[i],
comparisonString = comparisonString.replace(/['";:,.\/?\\-]/g, '');
RE = eval("/" + liveFilterValue + "/i");
if (comparisonString.match(RE)) {
rowsToShow.push(currentRow);
}
if(currentRow < liveFilterGridRows.length - 1) {
currentRow++;
} else {
currentRow = 0;
}
}
Many thanks for your time.
Chris
It is recommended to Use RegExp instead of eval.
DEMO
It will create
this.*sentence|sentence.*this/iremove
+'|'+words.reverse().join(".*")if you only want to findthis.....sentenceand notsentence....this