use regex to find strings between quotes in typescript

To find strings between quotes in a TypeScript string using regex, you need to create a regular expression that uses capturing groups to extract the desired text.

Here's an example:

index.ts
const text = 'The quick brown "fox" jumps over the lazy "dog"';

const regex = /"([^"]*)"/g;

const matches = [];

let match = regex.exec(text);

while (match !== null) {
  matches.push(match[1]);
  match = regex.exec(text);
}

console.log(matches); // Output: ["fox", "dog"]
276 chars
15 lines

In this example, we first create a TypeScript string with some text that contains words in quotes. Then, we create a regular expression that matches any text between two double quotes, using a capturing group that matches any character that is not a double quote (([^"]*)).

After that, we create an empty array to store our matches, and then use a while loop to execute the regular expression on the text and push each captured group to our matches array.

Finally, we log the array to the console to see the resulting matches.

Note that we use the g flag on the regular expression to indicate that we want to search for multiple matches, not just the first one.

gistlibby LogSnag