0

So many related questions out there, but none satisfyingly answered using javascript (no jQuery).

I wish to add quotes around a variable in a string I'm building. My string itself should contain single quotes within it like so:

'{'a'}'

I can get: '{a}'

When I try to add single quotes around the key a I get:

'{\'a\'}'

I've used both of the following syntax with the same result. Any suggestions??

concat('\'','a','\'')

'\''+'a'+'\''

See line 39 of this code: https://repl.it/@mike_butak/LiveForHisPleasureHeReallyIsThere

Thanks!

2
  • Can you show your code where you set the variable equal to a string, and also where/how are you seeing the output of your variable? Web console, alert(), console.log() ? Commented Jan 7, 2018 at 5:55
  • var b = "'" + a + "'"; Commented Jan 7, 2018 at 6:26

2 Answers 2

1

Like this?

console.log("'{'a'}'")

To expand on this, when you are building the string, just use " around the string, and ' within the string.

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

3 Comments

Thanks Chris. That'd work except it's just logging it instead of returning it.
Ah, if only it were that simple. I have to have a very dynamic construction process because it'll be used to create a large variety of different strings.
Thank you though!
1

Having a hard time replicating your issue! See:

   var temp = '\'{\'a\'}\'';
   console.log('s' + temp + 's');

I'd definitely recommend demonstrating the issue you are asking about in a console print or in a readily available editor online before posting a question!

As per your comment, updating to make it part of a variable assignment. Still unclear what the issue is!

6 Comments

It might be the difference that I'm returning the value as part of a string instead of console.logging it. Oh I just saw your recommendation to demonstrate using an online editor. Let me see what I can do. Thanks Peter!
Ok, the issue is on line 39 of this code: repl.it/@mike_butak/LiveForHisPleasureHeReallyIsThere
Just to show the same code in variable assignment, I updated! I'll check out your source in a sec!
I hope my answer helps! I tried to check your code, but I still don't quite track. Where do you expect there to be apostrophes?
I'm sorry Peter but when I visit the link now it says repl not found! I did change the name of it so that your changes wouldn't affect my working copy. I'm sorry but I've lost what you did in doing so.
|

Your Answer

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.