I need to implement JavaScript code for spelling check, where I want to perform text normalization which remove multiple whitespace before check spelling. However, in order to keep original text unchanged, I need to revert my normalization back to original text and readjust typo index.
I need to normalize, so I can cache the result and no need to run spelling check again next time. I just need to load the result from cache.
Here the following three steps:
- Replace multiple whitespace " " with only one " ".
// "I lik cat." -> "I lik cat"
// "I lik cat." -> "I lik cat"
const regex = / +/ig;
"I lik cat.".replaceAll(regex, ' ')
"I lik cat.".replaceAll(regex, ' ')
- Find location of word in cleaned text.
// "lik" start index is 2.
let regexp = /lik/g;
let str = 'I lik cat.';
let start_index = [];
let matches = [...str.matchAll(regexp)];
matches.forEach((match) => {
start_index.push(match.index);
});
- Revert cleaned code back to its original form and re-adjust start index accordingly.
"I lik cat" -> "I lik cat.", start_index = 6
"I lik cat" -> "I lik cat.", start_index = 2
I’m still struggling to find a solution for reverting normalized text to original?
2
Answers
The
replaceAll
method in JavaScript replaces all occurrences of a specified substring or pattern in a string with a new substring. However, it does not provide a built-in way to revert the replacement back to the original form.To revert the changes made by
replaceAll
, you would need to store the original string separately or keep track of the changes made during the replacement process. Then, you can use that information to restore the original string.Here’s an example of how you can revert the changes made by
replaceAll
using the original string:In the example above, we initially replace the substring "lik" with "love" using
replaceAll
to obtain the modified string. Then, we revert the replacement by usingreplaceAll
again to replace "love" back with "lik", resulting in the original string.Note that this approach assumes you have the original string stored separately or that you know the exact replacement that was made. It may not be feasible to revert the changes if you don’t have the original string or the specific replacement information.
Since you are doing a spell check with highlighting errors, you really don’t need to worry about spaces. For my answer, I use replace with a regex to replace the string wrapped in the mark tag and set a new variable to that value. This keeps the original string intact.