Home > Net >  Extract substring as number from string typescript
Extract substring as number from string typescript

Time:03-11

This is the format of the string:

enter image description here enter image description here

I want to extract the Revision number from the name. I tried using Substring() but I need a generic solution that could extract out Revision number from any name in this format. This is how the name is being set is the backend:

enter image description here

Thanks in advance.

CodePudding user response:

You can try this

const [,revisionNumber] = value.match(/Revision (\d)/)

CodePudding user response:

You could use a regex with a capturing group like this:

const input = 'Access2 - changed - (Revision 2)';

const regex = /^.*\(\w \s(\d )\)$/;

const [, revision] = regex.exec(input);

console.log(revision);

  • Related