1

I am trying to compare the variable using javascipt: response value: ""[email protected]""

response value i am getting it from server.

  var str1="[email protected]"
    var str2 =response;

if(str1===str2)
{
//
}

However not getting the proper result. any idea on how to compare them ?

9
  • 1
    ""[email protected]"" is invalid string... Commented Apr 22, 2016 at 5:51
  • '"[email protected]"'.indexOf('[email protected]') Commented Apr 22, 2016 at 5:51
  • @Rayon .. thanks .. yes, its an invalid string.Is there is a way we can change it?as indexOf having compatibility issues. Commented Apr 22, 2016 at 5:58
  • @RayonDabre: It's an invalid string literal, it's a perfectly fine string value. Since the OP says that the value is loaded from the server, it seems we are not dealing with a literal. Of course a complete example would be more useful. Commented Apr 22, 2016 at 5:59
  • @Harshit, indexOf() Commented Apr 22, 2016 at 6:00

2 Answers 2

2

There are a few ways to achieve your goal:

1) You can remove all " from the response when doing your equality check:

if(str1===str2.replace(/['"]+/g, ''))
{
//
}

2) Change your server code to not include ". Doing so, would mean that your Javascript will not need to change.

3) Last option, add " to your str1:

var str1='"[email protected]"'
var str2 =response;

if(str1===str2)
{
//
}

Obviously I don't know enough about your requirements to tell you which one you should do, but my suggestion would be choice #2 because I think it's strange to return an email address wrapped in quotes, otherwise I would recommend #1.

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

Comments

0

You are trying to compare '""[email protected]""' with '[email protected]'. They would never be equal.
Actually ""[email protected]"" is not a valid string. It might have been represented as '""[email protected]""' and "[email protected]" is a valid string (Same as '[email protected]').

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.