-1

I need to replace double quotes from string, in this case string declare to the variable like below

var string = "false";

I need convert like this,

var str = false;

because I need boolean value.

This is I have already tried,

var string= data.creditAllowed;
var str= string.replace(/"/g, '\\"');

but I have get like this,

str = "false";
7
  • If you're sure it's always a string "true" or "false", then just match it using ===. Commented Oct 20, 2014 at 9:13
  • If it is all about true or false, then you can simply use var str = (string == "true"); Commented Oct 20, 2014 at 9:13
  • 1
    double quotes are not part of your input string. Commented Oct 20, 2014 at 9:13
  • 1
    var str = string === 'false' ? false : true; would be a better solution. Commented Oct 20, 2014 at 9:15
  • 1
    @Andy: you can drop the ? false : true Commented Oct 20, 2014 at 9:20

2 Answers 2

2

The reason you're being down-voted is that you've misunderstood the way types are used in JavaScript. The following code:

var x = "thing";

Creates a string, containing the characters thing and binds the variable x to it. There are no quotes in the string. The quotes are a message to the parser that want to store a string.

Notice that when you log this value to the console, it puts quotes round the value to show it's a string, so it appears surrounded by quotes. These quotes are not stored.

The reason your replacement code doesn't work is that there are no quotes in the string in the first place.

If you wrote the following:

var y = "\"thing\"";

or

var z = '"thing"';

then you would have a string with quotes in it.

What you should be doing is parsing the string containing true. The quickest way is probably this:

function parseBool(input) {
  if (input == "true") { return true; }
  else if (input == "false") { return false; }
  else return null; // or false, or throw exception, or whatever
}
Sign up to request clarification or add additional context in comments.

7 Comments

explain the downvote please
That's not how you parse random input to a boolean...
Good. That's not what the question asks.
Then just return input === "true".
The point of my answer isn't the best way to parse a boolean string, it's to address the misunderstanding that led to the question. If someone had this genuine question, then it's worth addressing the underlying confusion. Also, my answer makes obvious the problems with parsing bad input and forces the questioner to think about them.
|
1

I think you should consider why the value is "false" instead of false instead.

Where is that value assigned, and is there any reason why you can't assign it as a proper boolean?

Otherwise you can do something like this:

function isStrTrueOrFalse(string) { return !string.toLowerCase().match(/false/);}

that way any string that is "false" returns false. Any other string returns true. This is because that "str" is true. No matter what the contents is.

You could also use the i flag in the regex, instead of .toLowerCase():

function isStrTrueOrFalse(string) { return !string.match(/false/i);}

As described in comments.

1 Comment

You could use the i flag in the regex, instead of toLowerCase()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.