在JavaScript字符串中转义单引号以进行JavaScript评估

时间:2022-10-28 15:15:31

I have a project, in which some JavaScript var is evaluated. Because the string needs to be escaped (single quotes only), I have written the exact same code in a test function. I have the following bit of pretty simple JavaScript code:

我有一个项目,其中评估了一些JavaScript var。因为字符串需要转义(仅限单引号),所以我在测试函数中编写了完全相同的代码。我有以下一些非常简单的JavaScript代码:

function testEscape() {
    var strResult = "";
    var strInputString = "fsdsd'4565sd";

    // Here, the string needs to be escaped for single quotes for the eval 
    // to work as is. The following does NOT work! Help!
    strInputString.replace(/'/g, "''");

    var strTest = "strResult = '" + strInputString + "';";
    eval(strTest);
    alert(strResult);
}

And I want to alert it, saying: fsdsd'4565sd.

我想提醒它,说:fsdsd'4565sd。

3 个解决方案

#1


69  

The thing is that .replace() does not modify the string itself, so you should write something like:

问题是.replace()不会修改字符串本身,所以你应该写如下:

strInputString = strInputString.replace(...

It also seems like you're not doing character escaping correctly. The following worked for me:

看起来你似乎没有正确地逃避角色。以下对我有用:

strInputString = strInputString.replace(/'/g, "\\'");

#2


2  

I agree that this var formattedString = string.replace(/'/g, "\\'"); works very well, but since I used this part of code in PHP with the framework Prado (you can register the js script in a PHP class) I needed this sample working inside double quotes.

我同意这个var formattedString = string.replace(/'/ g,“\\'”);效果非常好,但是因为我在PHP中使用了这部分代码和Prado框架(你可以在PHP类中注册js脚本)我需要在双引号中使用这个示例。

The solution that worked for me is that you need to put three \ and escape the double quotes. "var string = \"l'avancement\"; var formattedString = string.replace(/'/g, \"\\\'\");"

对我有用的解决方案是你需要放三个\并避开双引号。 “var string = \”l'avancement \“; var formattedString = string.replace(/'/ g,\”\\\'\“);”

I answer that question since I had trouble finding that three \ was the work around.

我回答这个问题,因为我很难找到三个是解决方法。

#3


-3  

strInputString = strInputString.replace(/'/g, "''");

#1


69  

The thing is that .replace() does not modify the string itself, so you should write something like:

问题是.replace()不会修改字符串本身,所以你应该写如下:

strInputString = strInputString.replace(...

It also seems like you're not doing character escaping correctly. The following worked for me:

看起来你似乎没有正确地逃避角色。以下对我有用:

strInputString = strInputString.replace(/'/g, "\\'");

#2


2  

I agree that this var formattedString = string.replace(/'/g, "\\'"); works very well, but since I used this part of code in PHP with the framework Prado (you can register the js script in a PHP class) I needed this sample working inside double quotes.

我同意这个var formattedString = string.replace(/'/ g,“\\'”);效果非常好,但是因为我在PHP中使用了这部分代码和Prado框架(你可以在PHP类中注册js脚本)我需要在双引号中使用这个示例。

The solution that worked for me is that you need to put three \ and escape the double quotes. "var string = \"l'avancement\"; var formattedString = string.replace(/'/g, \"\\\'\");"

对我有用的解决方案是你需要放三个\并避开双引号。 “var string = \”l'avancement \“; var formattedString = string.replace(/'/ g,\”\\\'\“);”

I answer that question since I had trouble finding that three \ was the work around.

我回答这个问题,因为我很难找到三个是解决方法。

#3


-3  

strInputString = strInputString.replace(/'/g, "''");