最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to Remove Special Character in JSON code output in html using JAVASCRIPT - Stack Overflow

programmeradmin0浏览0评论

What I need? I need in the Output:

kind: pagespeedonline#result id:://google/&q=EgRC-V0oGNT5k9AFIhkA8aeDS7H3-G0PVLS9rlyqHTECJPpug6NeMgFy

responseCode: 503

title: /

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

What are the issued? Currently, The output are like this,

 "kind": "pagespeedonline#result",
 "id": "://google/&q=EgRC-V0oGNT5k9AFIhkA8aeDS7H3-G0PVLS9rlyqHTECJPpug6NeMgFy",
 "responseCode": 503,
 "title": "/",
 "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": ""
        }
       ]
      }
     }
    ]
   },

What i tried?

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

But nothing happen.

Here is the file:

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",
    ";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-->

What I need? I need in the Output:

kind: pagespeedonline#result id:http://ipv4.google./sorry/index?continue=http://google./&q=EgRC-V0oGNT5k9AFIhkA8aeDS7H3-G0PVLS9rlyqHTECJPpug6NeMgFy

responseCode: 503

title: http://google./

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

What are the issued? Currently, The output are like this,

 "kind": "pagespeedonline#result",
 "id": "http://ipv4.google./sorry/index?continue=http://google./&q=EgRC-V0oGNT5k9AFIhkA8aeDS7H3-G0PVLS9rlyqHTECJPpug6NeMgFy",
 "responseCode": 503,
 "title": "http://google./",
 "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./speed/docs/insights/AvoidRedirects"
        }
       ]
      }
     }
    ]
   },

What i tried?

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

But nothing happen.

Here is the file:

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./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-->

I think i have some mistake in where to put the code to remove the special character on the output.

Share Improve this question edited Nov 10, 2017 at 11:53 flyx 39.9k9 gold badges105 silver badges150 bronze badges asked Nov 9, 2017 at 23:39 damikongaccontditodamikongaccontdito 1032 silver badges11 bronze badges 3
  • 3 Codepen demo has nothing to do with question – charlietfl Commented Nov 9, 2017 at 23:43
  • what does that have to do with string replacing? Just a waste of time looking at demos that are meaningless – charlietfl Commented Nov 9, 2017 at 23:59
  • @charlietfl - I edited my Question I hope this will help now. THanks – damikongaccontdito Commented Nov 10, 2017 at 0:47
Add a ment  | 

2 Answers 2

Reset to default 4

Can you not just use

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

This would replace all the invalid characters at once.

let str = '<pre> --all special character @#$%&*()\\??><{}[]</pre>'

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

console.log(str)

It is what you want, isn't it?

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./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-->

发布评论

评论列表(0)

  1. 暂无评论