0

I am looking for a way to RegEx match for a string (double quote followed by one or more letter, digit, or space followed by another double quote). For example, if the input was var s = "\"this is a string\"", I would like to create a RegEx to match this string and produce a result of [""this is a string""].

Thank you!

Joey
  • 1,144
  • 3
  • 15
  • 29
  • means instead of var s = "this is a string" this you want display ["this is a string"] like this ? – Devang Rathod Feb 09 '13 at 05:58
  • Look for a regex escaping library, and then use the `RegExp` constructor – John Dvorak Feb 09 '13 at 05:58
  • i'm not looking to pass a variable to a RegEx, just a pattern to match a double quote followed by one or more characters(letters, digits, spaces) followed by another double quote – Joey Feb 09 '13 at 16:21

3 Answers3

1

Use the RegExp constructor function.

var s = "this is a string";
var re = new RegExp(s);

Note that you may need to quote the input string.

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
0

This should do what you need.

s =~ /"[^"]*"/

The regex matches a double quote, followed by some number of non-quotes, followed by a quote. You'll run into problems if your string has a quote in it, like this:

var s = "\"I love you,\" she said"

Then you'll need something a bit more complicated like this:

s =~ /"([^"]|\\")*"/
George Madrid
  • 703
  • 7
  • 16
0

I just needed a pattern to match a double quote followed by one or more characters(letters, digits, spaces) followed by another double quote so this did it for me:

/"[^"]*"/

Joey
  • 1,144
  • 3
  • 15
  • 29