1

I have a string that will look something like this:

I'm sorry the code "codehere" is not valid

I need to get the value inside the quotes inside the string. So essentially I need to get the codehere and store it in a variable.

After some researching it looks like I could loop through the string and use .charAt(i) to find the quotes and then pull the string out one character at a time in between the quotes.

However I feel there has to be a simpler solution for this out there. Any input would be appreciated. Thanks!

Nathaniel Wendt
  • 1,194
  • 4
  • 23
  • 49
  • This question may be of some use http://stackoverflow.com/questions/413071/regex-to-get-string-between-curly-braces-i-want-whats-between-the-curly-braces – Trevor Aug 08 '11 at 16:56

8 Answers8

3

You could use indexOf and lastIndexOf to get the position of the quotes:

var openQuote  = myString.indexOf('"'),
    closeQuote = myString.lastIndexOf('"');

Then you can validate they are not the same position, and use substring to retrieve the code:

var code = myString.substring(openQuote, closeQuote + 1);
Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
  • Just on a side note - `substring` (as opposed to `substr`) accepts start and end so it could be a little bit more straightforward: `myString.substring(openQuote, closeQuote + 1)`. (The +1 is actually also obligatory in `substr` - now it does not contain the last `"`). – pimvdb Aug 08 '11 at 17:02
  • 1
    perfect! I don't know if I was clear enough in what I was asking for judging by the other answers, but this one was what I was looking for. Thanks! – Nathaniel Wendt Aug 08 '11 at 17:03
  • What if the string is something like this `I'm sorry the code "codehere" is not valid, "codethere" is valid` – ShankarSangoli Aug 08 '11 at 17:03
  • @ShankarSangoli - If that is the case then I agree, you need to use another method such as regular expressions. It depends on the application and where the string is coming from. – Justin Ethier Aug 08 '11 at 17:06
2

Regex:

var a = "I'm sorry the code \"codehere\" is not valid";
var m = a.match(/"[^"]*"/ig);
alert(m[0]);
Bob Fincheimer
  • 17,978
  • 1
  • 29
  • 54
1

Try this:

var str = "I'm sorry the code \"cod\"eh\"ere\" is not valid";
alert(str.replace(/^[^"]*"(.*)".*$/g, "$1"));
Chandu
  • 81,493
  • 19
  • 133
  • 134
1

You could use Javascript's match function. It takes as parameter, a regular expression. Eg:

/\".*\"/

Perception
  • 79,279
  • 19
  • 185
  • 195
Thomas Clayson
  • 29,657
  • 26
  • 147
  • 224
0

Use regular expressions! You can find a match using a simple regular expressions like /"(.+)"/ with the Javascript RegExp() object. Fore more info see w3schools.com.

Frog
  • 1,631
  • 2
  • 17
  • 26
0

Try this:

var msg = "I'm sorry the code \"codehere\" is not valid";
var matchedContent = msg.match(/\".*\"/ig);
//matchedContent is an array
alert(matchedContent[0]);
ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
0

You should use a Regular Expression. This is a text pattern matcher that is built into the javascript language. Regular expressions look like this: /thing to match/flags* for example, /"(.*)"/, which matches everything between a set of quotes.

Beware, regular expressions are limited -- they can't match nested things, so if the value inside quotes contains quotes itself, you'll end up with a big ugly mess.

*: or new RegExp(...), but use the literal syntax; it's better.

Sean McMillan
  • 10,058
  • 6
  • 55
  • 65
0

You could always use the .split() string function:

var mystring = 'I\'m sorry the code "codehere" is not valid' ;
var tokens = [] ;
var strsplit = mystring.split('\"') ;
for(var i=0;i<strsplit.length;i++) {
    if((i % 2)==0) continue;    // Ignore strings outside the quotes
    tokens.push(strsplit[i]) ;  // Store strings inside quotes.
}

// Output:
// tokens[0] = 'codehere' ;
Gus
  • 7,289
  • 2
  • 26
  • 23