mardi 30 mai 2017

regex test How to return only exact match

I have this code:

    var search_term = postAdminID

    // Wildcards Search
    var search = new RegExp(search_term, "i");
    sdsFilter = $.grep(sdsInfo.products, function(element, index) {
        var sted = search.exec(element.AdminID)

        return sted;

postAdminID is the index of a spesific post in an array.

element.AdminID is the corresponding index I am seraching for.

PROBLEM: Whenever I try to execute this search I get every matching index also parts of indexes matched.

Lets say I want to retrieve index #78. The above code returns: 78, 178, 278, 10078 and so forth. I only want the exact index - not every index containing the index.

I have tried .exec and test I have looked in every post here on stackoverflow that I can find.

Please help. How can I specify for regex that it should only look for the entire "string" ?

Aucun commentaire:

Enregistrer un commentaire