Use JavaScript to stripslashes ? possible

风格不统一 提交于 2019-12-03 14:36:14
ColBeseder

A new answer to an old question:

String.prototype.stripSlashes = function(){
    return this.replace(/\\(.)/mg, "$1");
}

Example of use:

var str = "You\'re slashed \/\\..\/\\"; // Text from server
str = str.stripSlashes() ;

output:

You're slashed /\../\

This is an old post but thought I would add my answer, seems more efficient than some other answers here:

var url = "http:\/\/www.divethegap.com\/update\/options\/padi-open-water\/"

var res = url.replace(new RegExp("\\\\", "g"), "");

This will replace all occurrences of a backslash character with nothing.

There has been a good port of many of php's core functions, including stripslashes over here: http://phpjs.org/functions/stripslashes/

function stripslashes (str) {
  // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // +   improved by: Ates Goral (http://magnetiq.com)
  // +      fixed by: Mick@el
  // +   improved by: marrtins
  // +   bugfixed by: Onno Marsman
  // +   improved by: rezna
  // +   input by: Rick Waldron
  // +   reimplemented by: Brett Zamir (http://brett-zamir.me)
  // +   input by: Brant Messenger (http://www.brantmessenger.com/)
  // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
  // *     example 1: stripslashes('Kevin\'s code');
  // *     returns 1: "Kevin's code"
  // *     example 2: stripslashes('Kevin\\\'s code');
  // *     returns 2: "Kevin\'s code"
  return (str + '').replace(/\\(.?)/g, function (s, n1) {
    switch (n1) {
    case '\\':
      return '\\';
    case '0':
      return '\u0000';
    case '':
      return '';
    default:
      return n1;
    }
  });
}

You're sending JSON, but inserting it directly into a HTML element. That is not wise, can create broken results, and probably not what you want to do in the first place.

You should probably either

  • change the PHP script's output to create proper HTML

  • expect JSON on the JavaScript side (using jQuery's dataType parameter, or the shorthand $.json(), and parse that

Have you tried string.replace?

success: function(data) {
 $('#OPTcontentpanel').load(data.OPTpermalink.replace("\\", ""));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!