使用Javascript为变量内部的值添加单引号 [英] Adding the single string quote for value inside variable using Javascript

查看:138
本文介绍了使用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;
}

并像这样使用它:

var king = addQuotes('king');  

console.log将显示:

console.log will display :

'king'

您可以在chrome/firefox控制台中尝试它,我做到了,当从此处复制/粘贴时,它可以完美地工作.

Edit : you can try it in the chrome/firefox console, I did it and it works perfectly when copy/paste from here.

这篇关于使用Javascript为变量内部的值添加单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆