use regex to find strings between single quotes in typescript in javascript

To find all strings between single quotes in a TypeScript or JavaScript string using regular expressions, you can use the following code:

index.ts
const myString = "Hello 'world' and 'universe'!";
const pattern = /'([^']+)'/g;
const matches = myString.match(pattern);

console.log(matches); // Output: ["'world'", "'universe'"]
181 chars
6 lines

Here, we define a regular expression pattern that looks for all single-quoted strings in the input string (/\'([^']+)\'/g), using a capturing group to match the contents between the quotes. We then call the String.match() function with this pattern to find all matches in the input string.

The matches variable will be an array of all matches found, which in this case will be ["'world'", "'universe'"]. You can then use these matches to perform whatever operations you need, such as replacing them with other strings or extracting the contents between the quotes.

gistlibby LogSnag