Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to find any double quoted string within a string

I'm trying to get a regex that will find a double quoted strings within a double quoted string. For example:

"some text "string 1" and "another string" etc"

I would like to pick out "string 1" and "another string".

I got as far as \"[^\"]*\" but this will match "some text " in the example. I basically need to ignore the first and last quotes and match within that.

Edit: The example mentioned doesn't have literal quotes surrounding it, but it is a Javascript string. The example regex is matching the entire string first. My Javascript is as follows.

var string = 'some "text" etc';
var pattern = new RegExp('\"[^\"]*\"/g');
var result = pattern.exec(string);
console.log("result: ", string);
// some "text" etc

So it could be my implementation of regex in Javascript that is the problem.

like image 907
Bryan Avatar asked Jan 19 '17 20:01

Bryan


1 Answers

Don't escape the ". And just look for the text between quotes (in non greedy mode .*?) like:

var string = 'some text "string 1" and "another string" etc';

var pattern = /".*?"/g;

var current;
while(current = pattern.exec(string))
    console.log(current);
like image 149
ibrahim mahrir Avatar answered Oct 12 '22 14:10

ibrahim mahrir