javascriptreactjsstringtesseracttesseract.js

How to get the result value number that maybe in any part of a string?


I use tesseract.js to get the image text in node js and in my case the strings are like this :

esr14

Tesseract rendered text :

ESR 14 fm

esr15

Tesseract rendered text :

ESR Less Than 10 mm/1st Hour 15

I want to get the ESR value which is under the the Results column and in the first case is after the ESR (14) string and in the second case is at the end of the string (15) .

I tried something like this with javascript split function which doesn't work for the second case and gives me the characters Le :

     let result;
          if (text.split("esr")[1]) {
            result = text.split("esr")[1];
          } else if (text.split("ESR")[1]) {
            result = text.split("ESR")[1];
          } else if (text.split("Esr")[1]) {
            result = text.split("Esr")[1];
          }

How can I get the actual result value in both cases and not get other characters in both strings ?


Solution

  • Maybe simply use regex for searching the last space followed by number?

    function checkESR(str){
      let matched = str.match(/ESR.*(\s[0-9]+)/);
      if(matched){
        return parseInt(matched[1]);
      }else{
        return null;
      }
    }
    
    console.log(checkESR("ESR Less Than 10 mm/1st Hour 15"));
    console.log(checkESR("ESR 14 fm"));
    console.log(checkESR("some other stuff before with result 11\nESR Less Than 10 mm/1st Hour 15\nsome stuff after with result 17"));