EN
TypeScript - get substring between quotes
0
points
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 aresult.