Home > front end >  Remove few words from string
Remove few words from string

Time:01-19

In my code I am calling Azure Function which calling data from AzureSQL. It returns in specific column string which looks like this -> Word, a. s.\1002: SomeWord\7010: AnotherWord\7300: AnotherOneWord\7304: LastWord. I wonder if it is possible to delete some words from string mentioned above, I would like to have only number 7010 (as string) in my string.

CodePudding user response:

function getNumber(column, word) {
  // matches "\<number>: <word>"
  const regex = new RegExp("\\\\([0-9] ): "   word.replace(/[.* ?^${}()|[\]\\]/g, '\\$&'));
  const match = regex.exec(column);

  if (match)
    return match[1];
}


const column = "Word, a. s.\\1002: SomeWord\\7010: AnotherWord\\7300: AnotherOneWord\\7304: LastWord";
const number = getNumber(column, "AnotherWord");

console.log(number); // "7010"

  • Related