Hello, help solve this problem. I put in variable texts in quotes. It looks to me like this var text = '"строка - 1""строка - 2""строка - 3"'; How can I pull out certain text in quotes from the text variable?

For example, I want to display such a line "строка - 1" or such "строка - 3"

How to do it?

  • The issue is resolved, I'm stupid, of course. It was necessary to use an array. - Anatoly

2 answers 2

 var text = '"строка - 1""строка - 2""строка - 3"'; var re = /(".*?")/g var strings = text.match(re); strings.forEach(function(item, i, arr){ alert(item); }); 

    You can use split () to split your line into components by separator " and then get the desired line as an array element using the formula Π½ΠΎΠΌΠ΅Ρ€ строки * 2 - 1 :

     var text = '"строка - 1""строка - 2""строка - 3"'; console.log('Массив ΠΏΠΎΠ»ΡƒΡ‡Π΅Π½Π½Ρ‹ΠΉ Π΄Π΅Π»Π΅Π½ΠΈΠ΅ΠΌ ΠΏΠΎ Π΄Π²ΠΎΠΉΠ½Ρ‹ΠΌ ΠΊΠ°Π²Ρ‹Ρ‡ΠΊΠ°ΠΌ:') console.log(text.split('"')); var inx = 2; console.log('Π‘Ρ‚Ρ€ΠΎΠΊΠ° Π½ΠΎΠΌΠ΅Ρ€ ' + inx + ':') console.log(text.split('"')[inx * 2 - 1]);