1

can you please tell me why application crash using this special character while searching a text on page .i am search a text from page .it is working fine but it is crash while using special character like this "(+?)" it goes to previous screen.. secondly \t.

Here is my 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 i = 0;

            $('.next_h').off('click').on('click', function () {
                i++;

                if (i >= $('.match').length) i = 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) i = $('.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 to where the first one 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");
    }


});
user2563256
  • 187
  • 1
  • 5
  • 14
  • possible duplicate of [Is there a RegExp.escape function in Javascript?](http://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript) – Felix Kling Jul 19 '13 at 10:51

1 Answers1

0

Those characters have special meanings in regex - they need to be escaped

Update - a VERY quick (and kinda dirty) fix:

var searchTermRegEx = new RegExp('('+searchTerm.replace(/([[\]()+{}?])/,'\\$1')+')', "ig");
MDEV
  • 10,730
  • 2
  • 33
  • 49
  • You are using regex to do the search - when you put `[]{}()+?` etc characters in a regular expression, they have special meanings and are parsed as a sort of function rather than plain text. In order to have them read as the character they are they must be 'escaped' which is basically putting a backslash `\ ` in front of them. The `.replace()` after searchTerm is making this change to the characters that need it – MDEV Jul 19 '13 at 11:00
  • But still same problem application back to previous screen..:( – user2563256 Jul 19 '13 at 12:10
  • application goes to previous screen while using q(?=u), q(?!u) (? <!a))b=abc character ..!! – user2563256 Jul 19 '13 at 12:16
  • What do you mean back to previous screen? When I use your original code and an unsupported character, either nothing happens or I get no results found – MDEV Jul 19 '13 at 13:05