我想删除字符串周围的“”。

例如,如果字符串是:“I am here”,那么我只想输出I am here。


当前回答

如果你想要老派,使用REGEX 1,$s/"//g

其他回答

如果你想要老派,使用REGEX 1,$s/"//g

这工作…

var string1 = "'foo'"; var string2 = '"bar"'; function removeFirstAndLastQuotes(str){ var firstChar = str.charAt(0); var lastChar = str[str.length -1]; //double quotes if(firstChar && lastChar === String.fromCharCode(34)){ str = str.slice(1, -1); } //single quotes if(firstChar && lastChar === String.fromCharCode(39)){ str = str.slice(1, -1); } return str; } console.log(removeFirstAndLastQuotes(string1)); console.log(removeFirstAndLastQuotes(string2));

如果只想从开头或结尾删除引号,请使用以下正则表达式:

'"Hello"'.replace(/(^"|"$)/g, '');

如果要删除字符串中的所有双引号,请使用

var str = '"some "quoted" string"';
console.log( str.replace(/"/g, '') );
// some quoted string

否则,你想要删除字符串周围的引号,使用:

var str = '"some "quoted" string"';
console.log( clean = str.replace(/^"|"$/g, '') );
// some "quoted" string

这是为懒人设计的一款简单的工具

var str = '"a string"';
str = str.replace(/^"|"$/g, '');