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.
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 !
Try using capturing groups like /“(.*?)”/g