Why can't I select this input field using this regex filter in jQuery?

So I am using the postolsey filter filter to try and select the dynamically generated nested form fields. Why do I keep getting an empty value for the test in the selector code below?

jQuery.expr[':'].regex = function(elem, index, match) {
    var matchParams = match[3].split(','),
        validLabels = /^(data|css):/,
        attr = {
            method: matchParams[0].match(validLabels) ? 
                        matchParams[0].split(':')[0] : 'attr',
            property: matchParams.shift().replace(validLabels,'')
        },
        regexFlags = 'ig',
        regex = new RegExp(matchParams.join('').replace(/^s+|s+$/g,''), regexFlags);
    return regex.test(jQuery(elem)[attr.method](attr.property));
}

//selector code
$(document).ready(function() { 
    var test = $('input:regex(id, agreement_activities_attributes_\d*_id)');
    console.log(test);
});

      

html code containing the input field. I am trying to choose

<input id="agreement_activities_attributes_0_id" name="agreement[activities_attributes][0][id]" type="hidden" value="28" />

      

+3


source to share


1 answer


There is an error in the code.

.replace(/^s+|s+$/g,'')

      

it should be



.replace(/^\s+|\s+$/g,'')

      

Then it should work.

The replacement removes all surrounding spaces (aligns the string). The space you used in the selector ( :regex(id, agreement_activities_attributes_\d*_id)

) will be included in the match pattern and thus will only match elements with an identifier that starts with a space.

+2


source







All Articles