How do you escape parentheses in jQuery selector?

后端 未结 3 483
天涯浪人
天涯浪人 2020-12-06 11:07

I am trying to check if a td innertext contains parentheses (). The reason is I display negative numbers as (1000) and I need to convert them to -1000 to do math. I\'ve trie

相关标签:
3条回答
  • 2020-12-06 11:46

    I don't use jQuery much, but the problem with your first one is that you're trying to put in text where it should be a selector - then you tried using a selector ":contains", but you then tried to escape the "(". Have you tried $("#fscaTotals td").filter(":contains('(')")? Use contains, but don't try to escape the parentheses.

    0 讨论(0)
  • 2020-12-06 12:03

    I think you'll have to use a filter function like:

    $('#fscaTotals td *').filter(function(i, el) {
        return !!$(el).text().match(/\(/);
    });
    

    Edit: I think this is a bug in jQuery's :contains().

    0 讨论(0)
  • 2020-12-06 12:03

    You can add a RegEx filter

    This technique is explained in this Blog Entry

    $.extend($.expr[':'], {  
        regex: function(a, i, m, r) {  
            var r = new RegExp(m[3], 'i');  
            return r.test(jQuery(a).text());  
        }  
    });
    

    Then you could use a regular expression like so.

    ("#fscaTotals td:regex('\([0-9]*\)')")
    

    By the way, I tested the RegEx example above with RegexBuddy and I think it is correct for your needs.

    0 讨论(0)
提交回复
热议问题