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!

3
  • means instead of var s = "this is a string" this you want display ["this is a string"] like this ? Commented Feb 9, 2013 at 5:58
  • Look for a regex escaping library, and then use the RegExp constructor Commented Feb 9, 2013 at 5: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 Commented Feb 9, 2013 at 16:21

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

It should be expanded beyond a note
Yup, I'm working on it...
Voted. That question always takes me a while to find.
you should replace the mataChars like var metaChars = /([[\]\*{}\^\$\\.|?+-])/g; var re = new RegExp(s.replace(metaChars, '\\$1'));
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 =~ /"([^"]|\\")*"/

1 Comment

I don't think bitwise-negating a regex is a good idea. Did you mistake a language?
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:

/"[^"]*"/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.