Generic
Generic

Reputation: 31

jQuery Regular Expression (regex)

Anyone help with this jQuery I'm trying to pull off I can't get the regex to work

cj("input[id='PRE_TEXT_'(.*)'_POST_TEXT']").hide();

Upvotes: 1

Views: 378

Answers (2)

James Montagne
James Montagne

Reputation: 78630

You can't use regex, but you should be able to use startswith and endswith.

cj("input[id^='PRE_TEXT_'][id$='_POST_TEXT']").hide();

Upvotes: 2

noob
noob

Reputation: 9202

You can't use regex in jQuery selectors. Use the filter function:

cj("input").filter(function() {
    if (/^PRE_TEXT_(.*)_POST_TEXT$/.test(cj(this).prop("id"))) {
        return true;
    }
    return false;
}).hide();

Upvotes: 3

Related Questions