我需要什么?我需要输出:
kind:pagespeedonline#results ID:http://ipv4.google.com/sorry/index?continue=http://google.com/&q=EgRC-V0oGNT5k9AFIhkA8aeDS7H3-G0PVLS9rlyqHTECJPpug6NeMgFy
responseCode:503
得分:98
pageStats: numberResources:10
numberHosts:4
totalRequestBytes:964
numberStaticResources:6
htmlResponseBytes:33184
cssResponseBytes:140515
imageResponseBytes:2701
javascriptResponseBytes:238456
otherResponseBytes:3511
numberJsResources:4
numberCssResources: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-->
我认为在将代码放在输出上的特殊字符的位置上我有一些错误。
答案 0 :(得分:3)
你能不能只使用
.replace(/[!@#$^&%*()+=[\]/{}|:<>?,.\\-]/g, '')
这将立即替换所有无效字符。
let str = '<pre> --all special character @#$%&*()\\??><{}[]</pre>'
str = str.replace(/[!@#$^&%*()+=[\]\/{}|:<>?,.\\-]/g, '')
console.log(str)
答案 1 :(得分:1)
这是你想要的,不是吗?
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-->