My javascript knowledge is limited. I use this code :
var myString = $("#builder-area-center-frame-content").html();
var pattern = /{{(.*?)}}/g;
var match;
while ((match = pattern.exec(myString)) !== null ){
$("#sim-edit-variable-replace").show();
$("#sim-edit-variable-replace select[name=variable_element]").append('<option value="'+ match[0] +'">'+ match[1] +'</option>');
}
'myString' is the container of my div #builder-area-center-frame-content, and in my div there are elements like that {{FOO}} or {{TOTO}}, etc...
I have a form with a select and in my loop i create option attribut for each {{XXX}} found. But if there are, for example, 2 {{FOO}}, the option attribut will be repeat twice. Like this example :
<select class="form-control" name="variable_element"><option value="{{FOO}}">FOO</option><option value="{{FOO}}">FOO</option><option value="{{toto}}">toto</option></select>
How can i remove duplicate {{FOO}} to keep only one ?
Sorry for my bad english, i hope you will understand what i want to do ?
EDIT : I tried your code but i have a lot of error of rendering about 'let'. I'm using NetBeans.
function variable_replace() {
let str = $("#builder-area-center-frame-content").html();
let collection = str.match(/{{[aA-zZ]+}}/g).filter((item, index, self) => index === self.indexOf(item));
if (collection.length > 0) {
$('#sim-edit-variable-replace').show();
let elem = $('#sim-edit-variable-replace select[name=variable_element]');
for (let item of collection) {
elem.append('<option value="${item.replace(/[{}]/g, '')}">${item}</option>');
}
}
}
variable_replace();