0

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.

1
  • 1
    So the "Revision" word can be translated? You're left with the parenthesis and the number to recognize the pattern, isn't it a bit too fragile? That being said I've provided an answer. Commented Mar 11, 2022 at 11:30

2 Answers 2

1

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);

Sign up to request clarification or add additional context in comments.

2 Comments

May I please know what does the regex group target?
the capturing group (unescaped parenthesis) surrounds the decimals that you need to retrieve, the result is an array with the full match, then the capturing group match. It retrieves the revision number when the input string is in the form of any characters(anyletters 123) where 123 can be one or more decimals
0

You can try this

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

2 Comments

Won't it throw exception when value is an empty string?
It will, to prevent that you can add || [] at the end of the line, then revisionNumber will simply be undefined

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.