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?

0

2 Answers 2

0

Using Regex for quoted string with escaping quotes:

(console\.log\("(?:[^"\\]|\\.)*"\),)
3
  • sample = 'console.log("Test regex =>"), abcdef'; sample.match('console\.log("(?:[^"\]|\\.)*"),')
    – iwan
    Jan 12, 2013 at 6: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, 2013 at 6:05
  • You're right nneonneo, i should have changed single quote to slash in Ruby. THANKS A LOT.
    – iwan
    Jan 12, 2013 at 6:43
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.

1
  • sorry, i dont understand the logic, are you assuming that inside double quote there is no comma?
    – iwan
    Jan 12, 2013 at 6:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.