0

I am a novice at Regex and would like to get some help on Regex non-capturing group.

('""dasgsa.txt dssdd "wqhweg.txt').match(/["']([^"']+)(txt)/gi) 

returns [""dasgsa.txt", "wqhweg.txt"] as expected.

('""dasgsa.txt dssdd "wqhweg.txt"').match(/(?:["']([^"']+))(txt)/gi)

returns [""dasgsa.txt", "wqhweg.txt"]

while I expected the result to be ["dasgsa.txt", "wqhweg.txt"]

Any help will be much appreciated.

sawa
  • 1,930
  • 3
  • 24
  • 33
  • Do you know that `regex.exec` does [something more](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec) than just 'matching' if you have the `global` flag? :) – swlim Jul 03 '16 at 14:19
  • I know that I can find successive matches with 'exec', but I wish I had my result without string concatenation. – sawa Jul 03 '16 at 14:23
  • Sorry, what do you mean by that? Do you just want `""dasgsa.txt dssdd "wqhweg.txt"` to be output as `["dasgsa.txt", "wqhweg.txt"]`? If you then want them to be in a same string, you can just call `.join()` on the array. – swlim Jul 03 '16 at 14:28
  • Perhaps it might be better to explain why you need this? So that people reading this might suggest a better way of doing it too. :) – swlim Jul 03 '16 at 14:29
  • If I go through the matches I get from exec(), I will be able to get the desired result with match[1] + match[2], but that is not what I want. I want to get my result without additional processing. My goal is just to learn Regex. – sawa Jul 03 '16 at 14:35
  • 1
    Ah I see! If learning is what you're after then I'd suggest reading up [MDN docs](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/RegExp) on the regex methods. Additionally, is [this something](https://jsbin.com/nopihusopo/1/edit?js,console) you are aiming for? – swlim Jul 03 '16 at 14:45
  • Yes, that is it! Thank you. Why don't you post it as an answer so that I can pick! – sawa Jul 03 '16 at 14:48
  • 1
    This question has been marked as a duplicate (due to the nature of your question), hence no answers can be posted. When people find this post, they can see the other one as well, and they'll pick it up from there to learn too, hopefully. – swlim Jul 03 '16 at 14:51

0 Answers0