text = '#container a.filter(.top).filter(.bottom).filter(.middle)';
regex = /(.*?)\.filter\((.*?)\)/;
matches = text.match(regex);
log(matches);
// matches[1] is '#container a'
//matchss[2] is '.top'
Я ожидаю захватить
matches[1] is '#container a'
matches[2] is '.top'
matches[3] is '.bottom'
matches[4] is '.middle'
Одним из решений было бы разделить строку на # контейнер a и отдохнуть. Затем отдохните и выполните рекурсивный exec, чтобы получить элемент inside ().
Обновление: Я публикую решение, которое действительно работает. Однако я ищу лучшее решение. Не очень нравится идея разделения строки и последующей обработки
Вот решение, которое работает.
matches = [];
var text = '#container a.filter(.top).filter(.bottom).filter(.middle)';
var regex = /(.*?)\.filter\((.*?)\)/;
var match = regex.exec(text);
firstPart = text.substring(match.index,match[1].length);
rest = text.substring(matchLength, text.length);
matches.push(firstPart);
regex = /\.filter\((.*?)\)/g;
while ((match = regex.exec(rest)) != null) {
matches.push(match[1]);
}
log(matches);
В поисках лучшего решения.