can please tell me why application crash using special character while searching text on page .i search text page .it working fine crash while using special character "(+?)" goes previous screen.. secondly \t.
here fiddle..? http://jsfiddle.net/4baau/5/
function searchandhighlight(searchterm, selector) { if (searchterm) { var selector = selector || "#realtimecontents"; var searchtermregex = new regexp('('+searchterm+')', "ig"); var matches = $(selector).text().match(searchtermregex); if (matches != null && matches.length > 0) { $('.highlighted').removeclass('highlighted'); $span = $('#realtimecontents span'); $span.replacewith($span.html()); var txt = $(selector).text().replace(searchtermregex, '<span class="match">$1</span>'); $(selector).html(txt); $('.match:first').addclass('highlighted'); var = 0; $('.next_h').off('click').on('click', function () { i++; if (i >= $('.match').length) = 0; $('.match').removeclass('highlighted'); $('.match').eq(i).addclass('highlighted'); $('.ui-mobile-viewport').animate({ scrolltop: $('.match').eq(i).offset().top }, 300); }); $('.previous_h').off('click').on('click', function () { i--; if (i < 0) = $('.match').length - 1; $('.match').removeclass('highlighted'); $('.match').eq(i).addclass('highlighted'); $('.ui-mobile-viewport').animate({ scrolltop: $('.match').eq(i).offset().top }, 300); }); if ($('.highlighted:first').length) { //if match found, scroll first 1 appears $(window).scrolltop($('.highlighted:first').position().top); } return true; } } return false; } $(document).on('click', '.searchbuttonclicktext_h', function (event) { $(".highlighted").removeclass("highlighted").removeclass("match"); if (!searchandhighlight($('.textsearchvalue_h').val())) { alert("no results found"); } });
those characters have special meanings in regex - need escaped
update - quick (and kinda dirty) fix:
var searchtermregex = new regexp('('+searchterm.replace(/([[\]()+{}?])/,'\\$1')+')', "ig");
Comments
Post a Comment