1

I can print out all the ng-show values, but I can't filter out what I want.
I don't know if it's a regular expression error or an angularjs. I tried it directly in the developer tools and the result is successful.
Please check the full code in codepen, thanks

var thList = $('thead').find('th');
        var fieldPattern = /\'(.*)\'.*\|.*/g;
        var allTableField = [];
        var valueList = [];
        angular.forEach(thList, function(th) {
            var value = $(th).attr('ng-show');
            console.log(value);
            if (/showField/g.test(value)) {
                valueList.push(value);
                var results = fieldPattern.exec(value);
                if (results) {
                    allTableField.push(results[1]);
                }
            }
        });

Codepen

Sorry, my English is terrible and difficult to express.
I want to get ng-show values through JQ and filter out values that match regular expressions.
In the end, I only got part of it.

enter image description here

Scheinin
  • 195
  • 9

1 Answers1

0

So your valueList contains more 'values' than your allTabField, the rest of the values are filtered by your regex fieldPattern

EDIT:

Use RegExp constructor like var fieldPattern = new RegExp("\'(.*)\'.*\|.*");

Alternative Solution:

angular.forEach(thList, function(th) {
                    var value = $(th).attr('ng-show');
                    console.log(value);
                    if (/showField/g.test(value)) {
                        valueList.push(value);
                        /* changes are made here */
                        var index = value.indexOf('|');
                        var results = value.substring(1, index - 1);
                        if (results) {
                            allTableField.push(results);
                        }
                        /* End of changes */
                    }
                });
Anouar Kacem
  • 635
  • 10
  • 24
  • Thank you very much for your answer. I use regex because it's possible to have spaces in `ng-show` values – Scheinin Dec 21 '18 at 16:25
  • @Scheinin you are welcome, I edited my answer this may work for you – Anouar Kacem Dec 21 '18 at 16:47
  • Thank you.`new RegExp("\'(.*)\'.*\|.*")` work for me.But I couldn't find the difference between `new RegExp("\'(.*)\'.*\|.*")` and `/\'(.*)\'.*\|.*/g`. – Scheinin Dec 21 '18 at 17:13
  • @Scheinin https://stackoverflow.com/questions/6149770/what-is-the-difference-between-using-new-regexp-and-using-forward-slash-notati check this question could be helpful – Anouar Kacem Dec 21 '18 at 17:16