Skip to content

Instantly share code, notes, and snippets.

@mssoylu
Last active April 28, 2017 02:02
Show Gist options
  • Save mssoylu/02f9b08af3074bed45930f6f6dcc43e5 to your computer and use it in GitHub Desktop.
Save mssoylu/02f9b08af3074bed45930f6f6dcc43e5 to your computer and use it in GitHub Desktop.
jquery regex fix table search
<input type="text" id="myInput" class="form-control mb5" placeholder="Sinav arayin...">
<div style="overflow: auto; max-height: 250px">
<table id="myTable">
{% for exam in exams %}
<tr>
<td width="1">
<input type="checkbox" name="examList[]" value="{{ exam.id }}">
</td>
<td>
{{ exam.code }} - {{ exam.name }}
</td>
</tr>
{% endfor %}
</table>
</div>
<script type="text/javascript">
function _fnEscapeRegex(sVal) {
var letters = {
"İ": "[İiIı]",
"I": "[İiIı]",
"Ş": "[ŞşSs]",
"S": "[ŞşSs]",
"Ğ": "[ĞğGg]",
"G": "[ĞğGg]",
"Ü": "[ÜüUu]",
"U": "[ÜüUu]",
"Ö": "[ÖöOo]",
"O": "[ÖöOo]",
"Ç": "[ÇçCc]",
"C": "[ÇçCc]",
"i": "[İiIı]",
"ı": "[İiIı]",
"ş": "[ŞşSs]",
"s": "[ŞşSs]",
"ğ": "[GgĞğ]",
"g": "[GgĞğ]",
"ü": "[ÜüUu]",
"u": "[ÜüUu]",
"ö": "[ÖöOo]",
"o": "[ÖöOo]",
"ç": "[ÇçCc]",
"c": "[ÇçCc]"
};
var acEscape = ['/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\', '$', '^', '-'];
var reReplace = new RegExp('(\\' + acEscape.join('|\\') + ')', 'g');
sVal = sVal.replace(reReplace, '\\$1');
return sVal.replace(/(([İIŞSĞÜUÇÖiışsğüuçö]))/g, function (letter) {
return letters[letter];
});
}
String.prototype.turkishToLower = function(){
var string = this;
var letters = { "İ": "i", "I": "ı", "Ş": "ş", "Ğ": "ğ", "Ü": "ü", "Ö": "ö", "Ç": "ç" };
string = string.replace(/(([İIŞĞÜÇÖ]))/g, function(letter){ return letters[letter]; })
return string.toLowerCase();
}
$(document).ready(function () {
$('#myTable tr').click(function (event) {
if (event.target.type !== 'checkbox') {
$(':checkbox', this).trigger('click');
}
});
$("#myInput").keyup(function () {
_this = this;
var keyword = $(_this).val().turkishToLower();
keyword = _fnEscapeRegex(keyword);
console.log(keyword);
$.each($("#myTable tbody tr"), function () {
if ($(this).text().turkishToLower().match(new RegExp(keyword))) {
console.log($(this).text().turkishToLower());
$(this).show();
}
else {
$(this).hide();
}
});
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment