0

Hello and thanks for coming by,

I have a string => "var string = Lets search for \"This film\" and \"This game\" ";

I would like => var result = ["This Film", "This game"];

How => with a Regex.

For the moment I do have something :

"Lets search for \"This film\" and \"This game\" ".match(/"([^}]*)"/)

Which return me

[""This film" and "This game"", "This film" and "This game"]

But that's not exactly what I would like.

Since I'm really confused with regex, (I know what this one is doing, but not the correct syntax to modify it), can someone help me achieve that?

Thank you very much

P.S : I do have searched on the internet, but all answer got this same exact problem.

Crocsx
  • 2,534
  • 1
  • 28
  • 50
  • 1
    Why does your regex have `[^}]` rather than `[^"]`? Also, you need a `g` flag on your regex. – nnnnnn Feb 22 '17 at 05:06
  • Oh it's been 20 minutes that i look at it and haven't seen this mistake ... Thank you, should take a break – Crocsx Feb 22 '17 at 05:11

2 Answers2

1

You need to use a lazy quantifier (*?) instead of a greedy quantifier (*):

Greedy quantifier *: Matches as many characters as possible.

Lazy quantifier: *?: Matches as few characters as possible.

In addition you need the global flag (g) in order to match multiple occurrences of the pattern. Your regex should be:

"Lets search for \"This film\" and \"This game\" ".match(/"([^}]*?)"/g)

You may find this question interesting: What do lazy and greedy mean in the context of regular expressions?

I'd recommend using Regex101 to play with the expression as well, they also have very good explanations regarding the quantifiers and everything else regex.

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
  • Thank you for the answer, I didn't knew about lazy and greedy quantifier, will remember it for net time. Just a further question, with your solution I do get and array but that look like => [""This film"", ""This game""] With double " this will probably cause problems, is it possible to remove the doubled " ? – Crocsx Feb 22 '17 at 05:13
  • The greedy/lazy thing is irrelevant here, because `[^}]` was a typo. With `[^"]*` even a greedy quantifier isn't going to match beyond the first `"` character that it finds. – nnnnnn Feb 22 '17 at 05:15
  • I agree with what you say it should be /"([^"]*?)"/g, but how is it possible that this do work ? /"([^}]*?)"/g In fact in the console it return "Lets search for \"This film\" and \"This game\" ".match(/"([^}]*?)"/g) [""This film"", ""This game""] – Crocsx Feb 22 '17 at 05:19
  • @Crocsx - It "works" because with the lazy `*?` there is still a `"` at the end of the regex that can match, but it only works because there are no `}` characters in the input. Fix the `}` and you don't need the `?`. – nnnnnn Feb 22 '17 at 05:28
1

var string = "Lets search for \"This film\" and \"This game\" ";
console.log(string);
console.log(string.match(/"([^"]*)"/g));

for more information read and play click regex101

Durgpal Singh
  • 11,481
  • 4
  • 37
  • 49
  • any way to have just Lets search for "This film" and "This game" [ "This film", "This game" ] ? – Crocsx Feb 22 '17 at 05:56