如何使用 JAVASCRIPT 在 html 格式的 JSON 代码输出中删除特殊字符



我需要什么?我需要在输出中:

种类:页面速度在线#结果id:http://ipv4.google.com/sorry/index?continue=http://google.com/&q=EgRC-V0oGNT5k9AFIhkA8aeDS7H3-G0PVLS9rlyqHTECJPpug6NeMgFy

响应代码:503

标题: http://google.com/

成绩: 98

页面统计:数量资源: 10

主机数量: 4

总请求字节数: 964

数量静态资源: 6

html响应字节: 33184

cssResponseBytes: 140515

图像响应字节: 2701

javascriptResponseBytes: 238456

其他响应字节:3511

编号Js资源: 4

数量Css资源: 1

发行了什么?目前,输出是这样的,

 "kind": "pagespeedonline#result",
 "id": "http://ipv4.google.com/sorry/index?continue=http://google.com/&q=EgRC-V0oGNT5k9AFIhkA8aeDS7H3-G0PVLS9rlyqHTECJPpug6NeMgFy",
 "responseCode": 503,
 "title": "http://google.com/",
 "score": 98,
 "pageStats": {
  "numberResources": 10,
  "numberHosts": 4,
  "totalRequestBytes": "964",
  "numberStaticResources": 6,
  "htmlResponseBytes": "33184",
  "cssResponseBytes": "140515",
  "imageResponseBytes": "2701",
  "javascriptResponseBytes": "238456",
  "otherResponseBytes": "3511",
  "numberJsResources": 4,
  "numberCssResources": 1
 },
 "formattedResults": {
  "locale": "en_US",
  "ruleResults": {
   "AvoidLandingPageRedirects": {
    "localizedRuleName": "Avoid landing page redirects",
    "ruleImpact": 0.0,
    "urlBlocks": [
     {
      "header": {
       "format": "Your page has no redirects. Learn more about avoiding landing page redirects.",
       "args": [
        {
         "type": "HYPERLINK",
         "value": "https://developers.google.com/speed/docs/insights/AvoidRedirects"
        }
       ]
      }
     }
    ]
   },

我试过什么?

var specialChars = "!@#$^&%*()+=-[]/{}|:<>?,.";
for (var i = 0; i < specialChars.length; i++) {
    stringToReplace = stringToReplace .replace(new RegExp("\" + specialChars[i], 'gi'), '');
}

但什么也没发生。

这是文件:

function clicked() {
  document.getElementById("data").innerHTML =
    "Fethching Score and Ranking <br>loading...";
  var xhr = new XMLHttpRequest();
  var url = document.getElementById("url").value;
  if (url == "") {
    alert("Please enter URL");
    return;
  }
  var xhr = new XMLHttpRequest();
  xhr.open(
    "GET",
    "https://www.googleapis.com/pagespeedonline/v1/runPagespeed?screenshot=false&strategy=mobile&url=" +
      encodeURIComponent(url)
  );
  
  xhr.onload = function() {
    document.getElementById("data").innerHTML = xhr.responseText;
  };
  xhr.send();
}
<input type="text" placeholder="Enter URL with http:// or https://" id="url" class="form-control"/>
<p>with <code>http</code> or <code>https</code></p>
<input type="button" value=" ENTER " onclick="clicked();"/>
 <pre id="data"></pre> <!--output json here-->

我想我在将代码放在哪里以删除输出上的特殊字符方面有一些错误。

你能不能只使用

.replace(/[!@#$^&%*()+=[]/{}|:<>?,.\-]/g, '')

这将立即替换所有无效字符。

let str = '<pre> --all special character @#$%&*()\??><{}[]</pre>'
str = str.replace(/[!@#$^&%*()+=[]/{}|:<>?,.\-]/g, '')
console.log(str)

这是你想要的,不是吗?

function clicked() {
  document.getElementById("data").innerHTML =
    "Fethching Score and Ranking <br>loading...";
  var xhr = new XMLHttpRequest();
  var url = document.getElementById("url").value;
  if (url == "") {
    alert("Please enter URL");
    return;
  }
  var xhr = new XMLHttpRequest();
  xhr.open(
    "GET",
    "https://www.googleapis.com/pagespeedonline/v1/runPagespeed?screenshot=false&strategy=mobile&url=" +
      encodeURIComponent(url)
  );
  
  xhr.onload = function() {
    document.getElementById("data").innerHTML = xhr.responseText.replace(/[!@#$^&%*()+=[]/{}|:<>?,.\-]/g, '');;
  };
  xhr.send();
}
<input type="text" placeholder="Enter URL with http:// or https://" id="url" class="form-control"/>
<p>with <code>http</code> or <code>https</code></p>
<input type="button" value=" ENTER " onclick="clicked();"/>
 <pre id="data"></pre> <!--output json here-->

最新更新