How to extract text between characters in regular expressions?

There is a line [123][456][qwerty]... Should be put into an array what is inside []. Tried .match( /[\[]{1}?(.+)[\]]{1}/ ), but it makes only the first, and the rest not, and if you add the /g, it is not the same.
I hope moderators will not be angry and will not delete my question, because in Google I search for do not know, a few hours can't find a real answer.
July 8th 19 at 15:28
4 answers
July 8th 19 at 15:30
Solution
var regexp = /([^\]\[]+)(?=\])/g,
 str = "[123][456][qwerty]",
result=str.match(regexp);
 alert(JSON.stringify(result));
Thank you! Can you tell me what is different (? ... ) from (?= ... ), I would like to kill the gap in my knowledge, the regular season always gets me in the soul - Nikita20 commented on July 8th 19 at 15:33
x(?=y) Find x, only if x follows y. a (? ... ) and this is incorrect syntax - yvette_Reichel commented on July 8th 19 at 15:36
July 8th 19 at 15:32
Solution
In my opinion more intuitive to do so:
var result = str.slice(1,-1).split('][');
Thought about that too, but the question included the phrase "regular expressions". - Nikita20 commented on July 8th 19 at 15:35
: I just don't like the regular season and unless absolutely necessary don't want friends - yvette_Reichel commented on July 8th 19 at 15:38
: If it fits , then why not. Even easier. - alphonso.Thomps commented on July 8th 19 at 15:41
July 8th 19 at 15:34
Solution
Only if you use exec in a loop. Something like this:
var regexp = /\[([^\]]+)\]/g,
 str = "[123][456][qwerty]",
current
result=[];
while ((current=regexp.exec(str)) != null){
result.push(current[1]);
}
I thought about it, decided to replace a $123$qwerty. Thanks for the reply! Wanted to ask you for a $123$qwerty there is a more logical option than /[^$]+/g ? And what is Greed in regexps? I can not understand. - Nikita20 commented on July 8th 19 at 15:37
this line better perhaps split to use
https://developer.mozilla.org/ru/docs/Web/JavaScri...
Greed is when the expression is selected the longest line possible.
When greedy '$wwewe$wewew$'.match(/\$.+\$/) choose the whole line, and with the lazy, '$wwewe$wewew$'.match(/\$.+?\$/) only the first occurrence, i.e. $wwewe$ - yvette_Reichel commented on July 8th 19 at 15:40
: that is, g is - greed? - alphonso.Thomps commented on July 8th 19 at 15:43
No, g is a global search for the entire string. Ie looks for all occurrences, not just the first one. And greed, to grab a string from a larger piece.
Enter the examples which were above in the js console, you'll see. - sally_Carro commented on July 8th 19 at 15:46
July 8th 19 at 15:36
Solution
Alternatively, you can do this:
var result = str.match( /\[([^\]]+)\]/ig ).map(n => n.slice(1,-1));
I never thought to trim the edges, thank you! - Nikita20 commented on July 8th 19 at 15:39
If there are no matches, a TypeError will be thrown out to null. - yvette_Reichel commented on July 8th 19 at 15:42
Yes. it is time to put the test - alphonso.Thomps commented on July 8th 19 at 15:45
: have noticed that the error appears. Put try {} catcha(e) {} - sally_Carro commented on July 8th 19 at 15:48
You condition to do: if str.match(/some/g) is not empty, then looping through the array .map om. - alphonso.Thomps commented on July 8th 19 at 15:51

Find more questions by tags Regular expressionsJavaScript