There are a number of elements in which you need to find all the phrases starting with #

For example, here is the html code itself:

 <div id="gallery"> <span class="portfolio-desc">Текст для примера #test_test1 #test2</span> <span class="portfolio-desc">Текст для примера #test2</span> <span class="portfolio-desc">Текст для примера #test_test1 #test_test5 #test2</span> </div> 

It is necessary to find all the phrases beginning with # without repetitions. Those. The script should output the following array:

 ('#test_test1', '#test2', '$test_test5') 

I tried to do this in a similar way, but I could not understand the regulars:

 var elementText = []; var a; $('#gallery span').each(function(i, selected){ if($(selected).text().match('#(.*)')){ elementText.push($(selected).text().match('#(.*)')); } }); a=elementText.join(); 

    1 answer 1

    Decided in this way:

     function unique(arr) { var result = []; nextInput: for (var i = 0; i < arr.length; i++) { var str = arr[i]; for (var j = 0; j < result.length; j++) { if (result[j] == str) continue nextInput; } result.push(str); } return result; } var elementText = []; var a; var rege = /#[а-яА-Яa-zA-Z_0-9]+/ig; $('#gallery span').each(function(i, selected){ if($(selected).text().match('#(.*)')){ elementText.push($(selected).text().match(rege)); } }); a=elementText.join();