Hi I have following scenario. I have working regex but cant pass it to jQuery selectors. I am trying following.
$(\"#prefix_[\\d]{1, 2}_postfix\")
You can use starts with and ends with attribute-value selector
$('[id^="prefix_"][id$="_postfix"]')
This will select all the elements whose id starts with prefix_ and ends with _postfix.
If the page contains many elements whose id starts with prefix_ and ends with _postfix but doesn't match the criteria that in between them should be one or two numbers, ex. , the starts with and ends with selector will not work. In this case filter can be used in combination with attribute selectors.
Demo
var regex = /^prefix_\d{1,2}_postfix$/;
// Narrow down elements by using attribute starts with and ends with selector
$('[id^="prefix_"][id$="_postfix"]').filter(function() {
// filter the elements that passes the regex pattern
return regex.test($(this).attr('id'));
}).css('color', 'green');
prefix_1_postfix
prefix_123_postfix
prefix_tushar_postfix
prefix__postfix
prefix_11_postfix
prefix_aa_postfix