I need a solution to my exceptional requirement. I have a string like
var mainString="[Avg Advisor Count by College numerator][Advising by college denominator] nominator count";
and have an array variable.
var searchArray = ["Advisor Count","Advising"];
I need to search the array elements in main string and if any of element match in main string then find the left first occurrence of the "[" of the string and right first occurrence of the "]" and encapsulate into a span.
As the first element of array finds in string "Advisor Count" and the second element also finds in the string. So the result would be
result = "<span>[Avg Advisor Count by College numerator]</span><span>[Advising by college denominator]</span>"
Can you please help me to find a solution for this in javascript.
What I tried, I just search the exact string of array in mainString. but don't get an idea of how to search occurrence of "[" in left and right of search string.
var mainString = calculatedFields["formula"][i];
calculatedArray.forEach(function (value) {
if (fm.indexOf('[' + value + ']') > -1)
fm = replaceAll(fm, '[' + value + ']', '<span style="color:#f29343">[' + value + ']</span>');
else {
fm = replaceAll(fm, value, '<span style="color:#f29343">' + value + '</span>');
}
});