Adding the single string quote for value inside variable using Javascript - javascript

I have a variable, before I use that variable,I need to add string quotes to the value/data which is inside the variable using JavaScript need help
//i am getting like this //
var variable=king;
//i want to convert it with single quote//
variable=variable;
but i need the data inside variable should be in a single quotation.

You can concatenate the variable with your quotes like :
function addQuotes(value){
var quotedVar = "\'" + value + "\'";
return quotedVar;
}
And use it like :
var king = addQuotes('king');
console.log will display :
'king'
Edit : you can try it in the chrome/firefox console, I did it and it works perfectly when copy/paste from here.

var x = 'abc';
var sData = "\'" + x +"\'";
// sData will print "'abc'"

var x = 'pqr';
var sData = "\'" + x +"\'";
// sData will print "'abc'"

1) You can use doublequotes
var variable = "'" + variable + "'";
2) ... or You can escape single quote symbol with backslash
var variable = '\'' + variable + '\'';

Related

Adding quotes to a comma separated string

What I have:
var a = "1.1.1.1,2.2.2.2,3.3.3.3"
What I need:
var a = '1.1.1.1','2.2.2.2','3.3.3.3'
What I'm trying:
var a = "1.1.1.1,2.2.2.2,3.3.3.3"
var b = a.split(",")
var c
for (var i=0;i<b.length; i++)
{
c.concat("\'").concat(b[i]).concat("\',\"")
}
What I'm actually getting with the above
"'1.1.1.1','"
I'm only able to get the first element right, how do I rectify this?
Also, in JS, is it even possible to have something like '1.1.1.1','2.2.2.2','3.3.3.3' stored in a variable?
A background to this problem:
I have an iframe whose source is a kibana query. The query in fact takes in values to a particular parameter in the above mentioned format.
Eg:
params:!('1.1.1.1','2.2.2.2')
While my db contains the param values as a string of CSV.
Eg.
"1.1.1.1,2.2.2.2,3.3.3.3"
Try this
var a = "1.1.1.1,2.2.2.2,3.3.3.3";
var b = "'" + a.split( "," ).join( "','" ) + "'";
console.log( b );
You don't need to deal with iterations for this, use a RegExp replace:
var a = "1.1.1.1,2.2.2.2,3.3.3.3";
var b = "'" + a.replace(/,/g, "','") + "'";
console.log( b );
The naive solution to your problem looks like this:
> line = '1.1.1.1,2.2.2.2,3.3.3.3'
'1.1.1.1,2.2.2.2,3.3.3.3'
> '"' + line.replace(/,/g, '","') + '"'
'"1.1.1.1","2.2.2.2","3.3.3.3"'
or if the quotes need to be reversed:
> "'" + line.replace(/,/g, "','") + "'"
'\'1.1.1.1\',\'2.2.2.2\',\'3.3.3.3\''
However, it sounds like what you need is a full-blown CSV parser, to handle cases in which you have quotes and commas and new lines and other crazy characters embedded in your input.
The naive solution seems to be in line, though, with what you were trying to do, and might illustrate why your approach fell short.
Your code works as you intended. Can you append to c without declaring?
var a = "1.1.1.1,2.2.2.2,3.3.3.3"
var b = a.split(",")
var c = ""
for (var i=0;i<b.length; b++)
{
c.concat("\'").concat(b[i]).concat("\',\"")
console.log(b)
}
You can store several values in a variables by using array for example.
If you want to get string like '"1.1.1.1","2.2.2.2","3.3.3.3"' you can use the following code:
var a = "1.1.1.1,2.2.2.2,3.3.3.3";
var b = a.split(',').map(function (str) {
return '"' + str+ '"';
}).join(',');
console.log(b);

Add double quotes to string which is stored in variable

I have variable which i am getting from DB string url, But the url does not have quotes to url i need to add the quotes to it below is my code.
var audioUrl
url is having string like http://xxxxx/xxx/xx-xx-123.m4a without double quotes
audioUrl= (data.url)
i need convert data.url value to "http://xxxxx/xxx/xx-xx-123.m4a"
Circle Jplayer
var audio="http://xxxxx/xxx/xx-xx-123.m4a"
var myOtherOne = new CirclePlayer("#jquery_jplayer_2",
{
m4a: audio,
}
If possible, I'd use ES6 syntax for this:
`"${data.url}"`
var audioUrl = "\""+ data.url+ "\"";
Whatever your get audioUrl and you want to wrap it with ", you need to put them and escape inner ones with . Above will result in:
"http://xxxxx/xxx/xx-xx-123.m4a"
OR if you are using the single quotes then no need to use the escape character.
var audioUrl = '"'+ data.url+ '"';
This is the simplest way
var audioUrl = '"' + (data.url) + '"'
This is a problem that comes up all the time for me and it does get frustrating.
A simple solution is to create global variables and use them in your strings as needed like so:
var singleQuote = " ' ";
var doubleQuote = ' " ';
Hope this helps others.

Change data inside string with box brackets

I have a string
garments[0][1]; // The 0 and 1 can be other numbers
I need to replace the data inside the second and the third box brackets.
[0] and [1]
So that it can be
garments[4][6]
Please let me know your suggestions when you get a chance, thank you.
You can try that:
var string = 'garments[' + 4 + '][' + 6 + ']'; //in your onClick function
//To increment dynamically:
var string = 'garments[' + i + '][' + j + ']'; //i and j being variables incrementing in your loops/treatments
Update to address comments:
If you want to break "garnments[0][1]" into "garnments",0 and 1 you can do the following:
var string = "garnments[0][1]";
string = string.split('['); //string = [["garnments"],["0]"],["1]"]]
string[1].replace(']','');
string[2].replace(']',''); //string = [["garnments"],["0"],["1"]]
You can then change values and rebuild your string for further use.
It is a bit brutal though. You can use RegExp as showed by #Diego
You can use String.prototype.replace()
'garments[0][1]'.replace('[0]','[4]').replace('[1]','[6]')
For any possible string with ***[m][n] format:
Function SetNewValues(testString, n, m)
{
var keyWordLengh = testString.indexOf("[");
return testString.substring(0,keyWordLengh) + "[" + n.toString() + "][" + m.toString() + "]";
}
Where:
testString is entire string to work on, like "something[342][345]"
n,m are values to be put inside brackets :)
This would be my approach.
var string = "['foobar'][2][12]";
var match =
/\[([^\]]+)\](?:\[(\d+)\])(?:\[(\d+)\])/g
.exec(string);
console.log(match);

Escape sequence jQuery for using variable inside quotes

I am trying to delete a column in table with the following.
var name = "1";
var name1= parseInt($name);
$('td:nth-child(name1)').hide();
This does not do anything. But if i hardcode it as
$('td:nth-child(1)').hide();
It deletes the column. Can anyone please suggest where i am going wrong?
Just concatenate the strings with the variables:
$('td:nth-child(' + name1 + ')').hide();
Javascript does not do string interpolation. So you need to concatenate your strings together like this:
var name = "1";
var name1= parseInt(name); // removed $ before variable name
$('td:nth-child(' + name1 + ')').hide();
Or, without doing converting to int, which isn't really necessary:
var name = "1";
$('td:nth-child(' + name + ')').hide();

variable inside another variable

hi there
i wanted to put this:
document.write("y = " + i);
inside another variable, like this:
s=s+"\"y = \" \+ i";
but now, i variable is not variable, just a plain text.
how can i fix this?
output would be
document.getElementById("output").innerHTML=s;
thank you
You do not pass to s string a variable - just text i. Try with:
s=s+"y = " + i;
You can also convert it to:
s += "y = " + i;

Categories

Resources