[javascript] Put quotes around a variable string in JavaScript

I have a JavaScript variable:

var text = "http://example.com"

Text can be multiple links. How can I put '' around the variable string?

I want the strings to, for example, look like this:

"'http://example.com'"

This question is related to javascript jquery string

The answer is


Try:

var text = "'" + "http://example.com" + "'";


let's think urls = "http://example1.com http://example2.com"

function somefunction(urls){
var urlarray = urls.split(" ");
var text = "\"'" + urlarray[0] + "'\"";
}

output will be text = "'http://example1.com'"


var text = "http://example.com";

text = "'"+text+"'";

Would attach the single quotes (') to the front and the back of the string.


You can escape " with \

var text="\"word\"";

http://jsfiddle.net/beKpE/


You can add these single quotes with template literals:

_x000D_
_x000D_
var text = "http://example.com"_x000D_
var quoteText = `'${text}'`_x000D_
_x000D_
console.log(quoteText)
_x000D_
_x000D_
_x000D_

Docs are here. Browsers that support template literals listed here.


To represent the text below in JavaScript:

"'http://example.com'"

Use:

"\"'http://example.com'\""

Or:

'"\'http://example.com\'"'

Note that: We always need to escape the quote that we are surrounding the string with using \

JS Fiddle: http://jsfiddle.net/efcwG/

General Pointers:

  • You can use quotes inside a string, as long as they don't match the quotes surrounding the string:

Example

var answer="It's alright";
var answer="He is called 'Johnny'";
var answer='He is called "Johnny"';
  • Or you can put quotes inside a string by using the \ escape character:

Example

var answer='It\'s alright';
var answer="He is called \"Johnny\"";
  • Or you can use a combination of both as shown on top.

http://www.w3schools.com/js/js_obj_string.asp


var text = "\"http://www.example1.com\"; \"http://www.example2.com\"";

Using escape sequence of " (quote), you can achieve this

You can place singe quote (') inside double quotes without any issues Like this

var text = "'http://www.ex.com';'http://www.ex2.com'"

Lets assume you have a bunch of urls separated by spaces. In this case, you could do this:

function quote(text) {
  var urls = text.split(/ /)
  for (var i = 0; i < urls.length; i++) urls[i] = "'" + urls[i] + "'"
  return urls.join(" ")
}

This function takes a string like "http://example.com http://blarg.test" and returns a string like "'http://example.com' 'http://blarg.test'".

It works very simply: it takes your string of urls, splits it by spaces, surrounds each resulting url with quotes and finally combines all of them back with spaces.


I think, the best and easy way for you, to put value inside quotes is:

JSON.stringify(variable or value)

This can be one of several solutions:

var text = "http://example.com";

JSON.stringify(text).replace('\"', '\"\'').replace(/.$/, '\'"')

In case of array like

result = [ '2015',  '2014',  '2013',  '2011' ],

it gets tricky if you are using escape sequence like:

result = [ \'2015\',  \'2014\',  \'2013\',  \'2011\' ].

Instead, good way to do it is to wrap the array with single quotes as follows:

result = "'"+result+"'";


Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to string

How to split a string in two and store it in a field String method cannot be found in a main class method Kotlin - How to correctly concatenate a String Replacing a character from a certain index Remove quotes from String in Python Detect whether a Python string is a number or a letter How does String substring work in Swift How does String.Index work in Swift swift 3.0 Data to String? How to parse JSON string in Typescript