JavaScript code to extract the words in quotations

Extracting text enclosed in quotation marks is a common task in programming, especially when working with strings that include quotes. In JavaScript, strings are a data type used to represent text. A string is a sequence of characters enclosed in single quotes ('), double quotes ("), or backticks (`).

JavaScript provides multiple ways to extract text enclosed in quotation marks. This article explains how to extract words or phrases inside quotations from a string efficiently using different approaches.

Methods to Extract Quoted Text

Extracting text enclosed in quotation marks can be done in the following ways:

Using Regular Expressions (RegEx)

Regular expressions provide an efficient way to extract words in quotations. Using regular expressions with the JavaScript match() method allows you to extract text in quotations with pattern matching.

Example

The following example uses regular expression with match() method to extract text within quotations:

const text = 'Hey there, "Welcome to Tutorialspoint!" we are "glad to see you here"';

// Regular Expression to match text between double quotes
const matches = text.match(/"([^"]*)"/g);

// Remove the surrounding quotes from each match
const extractedQuotes = matches.map(match => match.replace(/"/g, ""));

console.log(extractedQuotes);
[ 'Welcome to Tutorialspoint!', 'glad to see you here' ]

Code Explanation

Here's how the regular expression approach works:

  • The /"([^"]*)"/g pattern matches text between double quotes
  • [^"]* matches any character except double quotes
  • The g flag finds all matches in the string
  • map() removes surrounding quotes from each match
  • Returns an array of extracted quoted text

Using split() Method

Another approach is to split the string at each quotation mark and extract the relevant parts. The split() method divides the string into an array, then filter() selects the quoted content.

Example

The following example uses split() method to extract text within quotations:

const text = '"Tutorialspoint welcome you!" we are glad to see you here "Thanks for choosing us"';

// Split the string by double quotes
const parts = text.split('"');

// Extract every second element (text inside quotes)
const extractedQuotes = parts.filter((_, index) => index % 2 !== 0);

console.log(extractedQuotes);
[ 'Tutorialspoint welcome you!', 'Thanks for choosing us' ]

Code Explanation

Here's how the split method works:

  • split('"') divides the string at each double quote
  • Creates an array where quoted text appears at odd indices (1, 3, 5...)
  • filter((_, index) => index % 2 !== 0) selects odd-indexed elements
  • Returns an array containing only the quoted text

Handling Different Quote Types

You can modify these approaches to handle single quotes or mixed quotation marks:

const mixedText = `He said "Hello" and she replied 'Hi there!'`;

// Extract both single and double quoted text
const allQuotes = mixedText.match(/["']([^"']*)["']/g);
const cleanQuotes = allQuotes.map(match => match.slice(1, -1));

console.log(cleanQuotes);
[ 'Hello', 'Hi there!' ]

Comparison

Method Complexity Flexibility Best For
Regular Expression Medium High Complex patterns, mixed quotes
split() Method Low Medium Simple, consistent quote format

Conclusion

Both regular expressions and the split() method effectively extract quoted text from strings. Regular expressions offer more flexibility for complex patterns, while split() provides a simpler solution for straightforward cases. Choose the method that best fits your specific requirements.

Updated on: 2026-03-15T23:18:59+05:30

481 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements