最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Adding the single string quote for value inside variable using Javascript - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited Aug 25, 2016 at 13:39 Rafi Ud Daula Refat 2,25720 silver badges28 bronze badges asked Aug 25, 2016 at 12:25 kitty sarvajkitty sarvaj 5275 gold badges10 silver badges25 bronze badges 3
  • 1 How and from where do you retrieve the value king, Can you enclose it with '' there itself?\ – David R Commented Aug 25, 2016 at 12:29
  • 1 assuming the variable contains string, you can concatenate it using quotes: var variable = "'"+king+"'"; (note the single quotes between doubles) – Bart K Commented Aug 25, 2016 at 12:29
  • I am not sure what you are asking for . if you are not sure then parse it. variable.toString(). and if you want string quote you can do it. variable = '/''+ variable + '/''; – Rafi Ud Daula Refat Commented Aug 25, 2016 at 12:30
Add a ment  | 

4 Answers 4

Reset to default 1

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 + '\'';
发布评论

评论列表(0)

  1. 暂无评论