JQuery-selector-like regex

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'

I expect a capture

matches[1] is '#container a'
matches[2] is '.top'
matches[3] is '.bottom'
matches[4] is '.middle'

One solution would be to break the line into #container a and rest. Then take a break and execute recursive exec to get the element inside ().

Update: I am posting a solution that really works. However, I am looking for a better solution. Don't really like the idea of ​​splitting a string and then processing Here is a solution that works.

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);

Look for the best solution.

+3
source share
5 answers

This will follow the one example you posted:

<html>
  <body>
    <script type="text/javascript">
      text = '#container a.filter(.top).filter(.bottom).filter(.middle)';
      matches = text.match(/^[^.]*|\.[^.)]*(?=\))/g);
      document.write(matches);
    </script>
  </body>
</html>

which produces:

#container a,.top,.bottom,.middle 

EDIT

Here is a short explanation:

^         # match the beginning of the input
[^.]*     # match any character other than '.' and repeat it zero or more times
          #
|         # OR
          #
\.        # match the character '.'
[^.)]*    # match any character other than '.' and ')' and repeat it zero or more times
(?=       # start positive look ahead
  \)      #   match the character ')'
)         # end positive look ahead

EDIT Part II

:

  • , ., : ^[^.]*
  • , ., , . ), \.[^.)]*, ) : (?=\)). .filter .
+5

-, .

var head, filters = [];
text.replace(/^([^.]*)(\..*)$/, function(_, h, rem) {
  head = h;
  rem.replace(/\.filter\(([^)]*)\)/g, function(_, f) {
    filters.push(f);
  });
});
console.log("head: " + head + " filters: " + filters);

String.replace Javascript: -)

+3

, (. while https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/RegExp/exec):

"g", exec , . , str, lastIndex. , , script:

var myRe = /ab*/g;
var str = "abbcdefabh";
var myArray;
while ((myArray = myRe.exec(str)) != null)
{
  var msg = "Found " + myArray[0] + ".  ";
  msg += "Next match starts at " + myRe.lastIndex;
  print(msg);
}

script :

Found abb. Next match starts at 3
Found ab. Next match starts at 9

, . , .

0
var text = '#container a.filter(.top).filter(.bottom).filter(.middle)';
var result = text.split('.filter');

console.log(result[0]);
console.log(result[1]);
console.log(result[2]);
console.log(result[3]);
0
source

text.split() with a regular expression does the trick.

var text = '#container a.filter(.top).filter(.bottom).filter(.middle)';
var parts = text.split(/(\.[^.()]+)/);
var matches = [parts[0]];

for (var i = 3; i < parts.length; i += 4) {
    matches.push(parts[i]);
}

console.log(matches);
0
source

Source: https://habr.com/ru/post/1734924/


All Articles