0

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!

asked Aug 8, 2011 at 16:53
1

8 Answers 8

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);
answered Aug 8, 2011 at 16:58
Sign up to request clarification or add additional context in comments.

4 Comments

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 ").
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!
What if the string is something like this I'm sorry the code "codehere" is not valid, "codethere" is valid
@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.
2

Regex:

var a = "I'm sorry the code \"codehere\" is not valid";
var m = a.match(/"[^"]*"/ig);
alert(m[0]);
answered Aug 8, 2011 at 16:56

Comments

1

Try this:

var str = "I'm sorry the code \"cod\"eh\"ere\" is not valid";
alert(str.replace(/^[^"]*"(.*)".*$/g, "1ドル"));
answered Aug 8, 2011 at 16:55

1 Comment

What if there is nothing that matches that pattern? then the whole string will be returned?
1

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

/\".*\"/

Perception
80.8k19 gold badges190 silver badges197 bronze badges
answered Aug 8, 2011 at 16:56

Comments

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.

answered Aug 8, 2011 at 16:56

2 Comments

w3schools contains lots of errors, I would not advise to use it.
You probably shouldn't recommend w3schools. A lot of their material is misleading and sometimes downright incorrect. See.. w3fools.com
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]);
answered Aug 8, 2011 at 16:58

Comments

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.

answered Aug 8, 2011 at 17:11

Comments

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' ;
answered Aug 8, 2011 at 17:19

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.