I am developing a feature in my app that searches through the text and abbreviates words/phrases while the user is inputting the text.
let dictionary = {
"M": {
"Mobile telephone number": "M/TEL"
},
"T": {
"Telephone": "TEL"
}
};
function abbreviateText() {
const input = document.getElementById("input").value;
let output = input;
for (const groupKey in dictionary) {
const group = dictionary[groupKey];
for (const key in group) {
const regex = new RegExp(`\b${key}\b`, "gi");
const replacement = `<span class="tooltip" data-tooltip="${key}">${group[key]}</span>`;
output = output.replace(regex, replacement);
}
}
document.getElementById("output").innerHTML = output;
}
<textarea id="input" placeholder="Enter message" rows="7" oninput="abbreviateText()"></textarea>
<p id="output"></p>
When inputting mobile telephone number
, it should output M/TEL
instead of TEL number">M/TEL
.
The source output should be <span class="tooltip" data-tooltip="Mobile telephone number">M/TEL"</span>
. Note that there is not a span
tag encompassing TEL
.
I have other instances of this occurring so the solution needs to be general.
2
Answers
If you are sure that your initial input does not contain HTML you can use 2 step replacement.
Also you need to make sure you have "longer" replacements checking first.
You can tokenize the whole output and then process the tokens one by one:
To avoid unnecessary iteration, changing the data structure to something like this is essential:
…or you can afford removing the grouping altogether so that the second step is unnecessary:
Try it: