Repeated return of the returned group without capture

I had a strange problem that I canโ€™t get around ... In principle, I have this Regex that returns material that I donโ€™t have in the capture group, and it does not return the actual captured group. Here is the regex:

"localhost/a/b/c".match(/\/a\/b\/(.*?)/g);

As far as I know, it is supposed to return ["c"]... But it returns:

["/a/b/"]

What am I doing wrong? I thought that the captured groups should have been returned, not ignored.

+4
source share
1 answer

Try: "localhost / a / b / c" .match (// a / b / (. +) /) [1]

In the original regular expression "localhost/a/b/c".match(/\/a\/b\/(.*?)/g);

  • . means any character
  • * , 0 -
  • ? , ,
  • , , g, , .
  • , [0] - , [1] - ..

$('#myDiv').append( "localhost/a/b/c".match(/\/a\/b\/(.+)/)[1] );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="myDiv"><div>
+6

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


All Articles