Languages
[Edit]
EN

TypeScript - get substring between quotes

0 points
Created by:
Welsh
902

In this article, we would like to show you how to get substring between quotes in TypeScript.

Quick solution:

const matches: string[] = text.match(/"(.*?)"/g); // array of substrings surrounded with ""

 

Practical example

In this example, we use match() method with /"(.*?)"/g regex, which matches everything in between quotation marks (""). This way we get an array of substrings as a result.

const text: string = 'Example words: "ABC" and "DEF".';

const matches: string[] = text.match(/"(.*?)"/g);

if (matches) {
  for (let i = 0; i < matches.length; ++i) {
    const match = matches[i];
    const substring = match.substring(1, match.length - 1); // quotation mark removing
    console.log(substring);
  }
}

Output:

ABC
DEF

Note:

If you remove the global flag (/g) from the regex, you will receive only the first substring as a result.

See also

  1. JavaScript - get substring between quotes

Alternative titles

  1. TypeScript - find text between quotes
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join