Reputation: 2587
I didnt write the code below but ive added an if to it, to check for a value called starts, if starts is set to 1, I only want to return results that start with the value, i tried searching RegEx exp and swapped out the 'i' for a 'a' or '\A' but neither work, what character do i need in there for a starts with?
Thanks
function searchTable(inputVal, tablename,starts) {
var table = $(tablename);
table.find('tr:not(.header)').each(function (index, row) {
var allCells = $(row).find('td');
if (allCells.length > 0) {
var found = false;
allCells.each(function (index, td) {
if (starts = 1) {
var regExp = new RegExp(inputVal, 'i');
} else {
var regExp = new RegExp(inputVal, 'i');
}
if (regExp.test($(td).text())) {
found = true;
return false;
}
});
if (found == true) $(row).show().removeClass('exclude'); else $(row).hide().addClass('exclude');
}
});
}
Upvotes: 0
Views: 363
Reputation: 8293
You need to use the caret ^
at the beginning of your regexp (it matches the position "beginning of a string or a line"). But that's not a modifier. The i
pour replaced stands for "case insensitive".
So...
var regExp = new RegExp((starts ? '^' : '') + inputVal, 'i');
Upvotes: 2