2

How to avoid Object is possibly null in VSCode though code runs ok

  let url = 'https://test.com/test/varstring/stringtoextract?id=test3'
  let regex = /https:\/\/test.com\/test\/varstring\/.+\/(.+)?.+/
  var match = regex.exec(url);
  alert(match[1]); 

1 Answer 1

2

If you are sure, use the Non-null assertion operator !:

alert(match![1]);

Of course, an if statement would also do the trick:

if (match) {
    alert(match[1]);
}
Sign up to request clarification or add additional context in comments.

Comments

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.