1

I'm trying to use regular expressions in TypeScript to

  1. Check if a given string matches an expression
  2. Extract a substring

These are the two strings I'm trying to match against Total Order <orderId> ex: Total Order order1, I should be able to extract order1 from the regular expression

The other string I'm trying to match against is. I have been trying to do this without any success, I came across the following but no success How do you access the matched groups in a JavaScript regular expression?, but this is different from what I'm trying to do.

Items Order <orderId>, the could have spaces as well ex: Items Order order1. I should be able to extract order1 from this again.

pandith padaya
  • 1,643
  • 1
  • 10
  • 20

1 Answers1

5

You can use Regexp Match:

const str = 'Items Order order1';
const matches = str.match(/Items\sOrder\s(\w+)/);
if (matches) {
    console.log(matches[1]); // ordem1    
}

if the matches variable is filled, then a valid content will be available in the second position of the array (group captured).

Rodrigo Brito
  • 378
  • 3
  • 9