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

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


当前回答

var expressionWithoutQuotes = '';
for(var i =0; i<length;i++){
    if(expressionDiv.charAt(i) != '"'){
        expressionWithoutQuotes += expressionDiv.charAt(i);
    }
}

这可能对你有用。

其他回答

str = str.replace(/^"(.*)"$/, '$1');

只有当引号是字符串的第一个和最后一个字符时,这个regexp才会删除引号。F.ex:

"I am here"  => I am here (replaced)
I "am" here  => I "am" here (untouched)
I am here"   => I am here" (untouched)

请尝试使用regex从字符串中删除双引号。

    $string = "I am here";
    $string =~ tr/"//d;
    print $string;
    exit();

如果你只想删除边界引号:

function stripquotes(a) {
    if (a.charAt(0) === '"' && a.charAt(a.length-1) === '"') {
        return a.substr(1, a.length-2);
    }
    return a;
}

如果字符串看起来不像“带引号的文本”,这种方法不会触及字符串。

如果字符串保证在开头和结尾有一个引号(或任何其他单个字符),你想删除:

str = str.slice(1, -1);

Slice的开销比正则表达式小得多。

这个简单的代码也可以工作,例如从双引号包围的字符串中删除双引号:

var str = 'remove "foo" delimiting double quotes';
console.log(str.replace(/"(.+)"/g, '$1'));