使用.val()时需要帮助转换为“to”

时间:2015-11-16 17:43:58

标签: jquery html json regex

我使用jquery来获取隐藏输入的值。隐藏输入包含带有JSON对象的data-trix-attachment属性。问题是jquery用"替换了引号。我不知道为什么。

我已经四处寻找解决方案了,似乎无法找到解决方案。

我尝试使用正则表达式将"替换为',但这也不起作用。 (下面是一个例子)

任何人都可以帮我解决这个问题。以下是我尝试使用的代码。

    var new1 = $("#trix-input-1").attr("value");
   var e_encoded = new1.replace(/"/g, """); 

HTML代码

<a contenteditable="false" href="http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png" data-trix-attachment="{"contentType":"image/png","filename":"Screen Shot 2015-11-16 at 11.36.45 AM.png","filesize":9291,"height":77,"href":"http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png","url":"http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png","width":305}" data-trix-content-type="image/png" data-trix-id="38">

jQuery响应

<a data-trix-content-type=&quot;image/png&quot; data-trix-attachment=&quot;{&quot;contentType&quot;:&quot;image/png&quot;,&quot;filename&quot;:&quot;Screen Shot 2015-11-16 at 11.36.45 AM.png&quot;,&quot;filesize&quot;:9291,&quot;height&quot;:77,&quot;href&quot;:&quot;http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png&quot;,&quot;url&quot;:&quot;http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png&quot;,&quot;width&quot;:305}&quot; href=&quot;http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png&quot;><figure class=&quot;attachment attachment-preview png&quot;><img src=&quot;http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png&quot; height=&quot;77&quot; width=&quot;305&quot;><figcaption class=&quot;caption&quot;>Screen Shot 2015-11-16 at 11.36.45 AM.png <span class=&quot;size&quot;>9.07 KB</span></figcaption></figure></a>

jQuery代码

   var new1 = $("#trix-input-1").val();

1 个答案:

答案 0 :(得分:2)

replace的语法错误,它的replace(oldStr, newStr)反之亦然。见这里:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/replace

所以这个:

var e_encoded = new1.replace(/"/g, "&quot;"); 

应该是:

var e_encoded = new1.replace(/&quot;/g, "'"); 

你的例子很好用:

&#13;
&#13;
console.log('<a data-trix-content-type=&quot;image/png&quot; data-trix-attachment=&quot;{&quot;contentType&quot;:&quot;image/png&quot;,&quot;filename&quot;:&quot;Screen Shot 2015-11-16 at 11.36.45 AM.png&quot;,&quot;filesize&quot;:9291,&quot;height&quot;:77,&quot;href&quot;:&quot;http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png&quot;,&quot;url&quot;:&quot;http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png&quot;,&quot;width&quot;:305}&quot; href=&quot;http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png&quot;><figure class=&quot;attachment attachment-preview png&quot;><img src=&quot;http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png&quot; height=&quot;77&quot; width=&quot;305&quot;><figcaption class=&quot;caption&quot;>Screen Shot 2015-11-16 at 11.36.45 AM.png <span class=&quot;size&quot;>9.07 KB</span></figcaption></figure></a>'.replace(/&quot;/g, "'"))
&#13;
&#13;
&#13;

使用document.write而不是console output:

&#13;
&#13;
document.write('<a data-trix-content-type=&quot;image/png&quot; data-trix-attachment=&quot;{&quot;contentType&quot;:&quot;image/png&quot;,&quot;filename&quot;:&quot;Screen Shot 2015-11-16 at 11.36.45 AM.png&quot;,&quot;filesize&quot;:9291,&quot;height&quot;:77,&quot;href&quot;:&quot;http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png&quot;,&quot;url&quot;:&quot;http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png&quot;,&quot;width&quot;:305}&quot; href=&quot;http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png&quot;><figure class=&quot;attachment attachment-preview png&quot;><img src=&quot;http://localhost/gu/attachments/2015-11-161447695275991-Screen Shot 2015-11-16 at 11.36.45 AM.png&quot; height=&quot;77&quot; width=&quot;305&quot;><figcaption class=&quot;caption&quot;>Screen Shot 2015-11-16 at 11.36.45 AM.png <span class=&quot;size&quot;>9.07 KB</span></figcaption></figure></a>'.replace(/&quot;/g, "'"))
&#13;
&#13;
&#13;