Categories
JavaScript Answers

How to get text between double quotes with JavaScript?

Spread the love

Sometimes, we want to get text between double quotes with JavaScript.

In this article, we’ll look at how to get text between double quotes with JavaScript.

How to get text between double quotes with JavaScript?

To get text between double quotes with JavaScript, we can use a regex.

For instance, we write:

const s = `"hello world"`
const [_, text] = s.match(/"((?:\\.|[^"\\])*)"/)
console.log(text)

to call s.match with /"((?:\\.|[^"\\])*)"/ to return an object with the text between the double quotes in the 2nd entry.

Therefore, text is 'hello world'.

Conclusion

To get text between double quotes with JavaScript, we can use a regex.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

2 replies on “How to get text between double quotes with JavaScript?”

hello,

how about if there are 2 double quotes in one sentence. for example

var text = ‘My name is “Sarah” and I live in “Toronto”

I want to extract both ‘Sarah’ and ‘Toronto’ and save it in an array. Hope you can help me with this !

Leave a Reply

Your email address will not be published. Required fields are marked *