0

I need to extract using Regular Expression from following string

console.log("This can be anything except double quote"),

followed by comma and any other string

and the extraction output is

console.log("This can be anything except double quote"),

Note that the sample string shall not be read literally (e.g. can be anything means a random string or symbol

~!@#$%^&*)

Any idea, what is the right regular expression for above case?

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
iwan
  • 7,269
  • 18
  • 48
  • 66

2 Answers2

0

There are many solutions to this. The simplest I could think of: (console.log[^,]+)

PS: This removes the comma at the end of the console statement. You can manually add that.

Srinivas
  • 1,780
  • 1
  • 14
  • 27
  • sorry, i dont understand the logic, are you assuming that inside double quote there is no comma? – iwan Jan 12 '13 at 06:06
0

Using Regex for quoted string with escaping quotes:

(console\.log\("(?:[^"\\]|\\.)*"\),)
Community
  • 1
  • 1
nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • sample = 'console.log("Test regex =>"), abcdef'; sample.match('console\.log\("(?:[^"\\]|\\.)*"\),') – iwan Jan 12 '13 at 06:04
  • thank nneonneo, i tested in ruby with above 2 lines code , it gave me error "premature end of char-class: /console\.log\("(?:[^"\]|\.)*"\),/ (RegexpError)" – iwan Jan 12 '13 at 06:05
  • You're right nneonneo, i should have changed single quote to slash in Ruby. THANKS A LOT. – iwan Jan 12 '13 at 06:43