skip to Main Content

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:

  1. 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, ' ')

  1. 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);
    });
  1. 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


  1. 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:

    const originalString = "I lik cat.";
    const modifiedString = originalString.replaceAll("lik", "love");
    
    // Revert the changes made by replaceAll
    const revertedString = modifiedString.replaceAll("love", "lik");
    
    console.log(revertedString); // Output: "I lik cat."
    

    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 using replaceAll 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.

    Login or Signup to reply.
  2. 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.

    const output = document.querySelector(".output");
    const original = document.querySelector(".original");
    const regexp = /(lik)/ig;
    
    let str = "I lik C                   AT. A                      cat I lik.";
    
    original.innerHTML = str;
    
    const newOutput = str.replace(regexp,"<mark>$1</mark>"); 
    output.innerHTML = newOutput;
    mark {
      background: transparent;
      border-bottom: 1px solid #ff0000;
    }
    <div class="output"></div>
    <div class="original"></div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search