简体   繁体   English

如何去除url中的空格

[英]How to remove empty spaces in the url

Hi how can i remove empty space in the URL using javascript: here how it looks like嗨,我如何使用 javascript 删除 URL 中的空白空间:这里看起来像

stockcode=1ECN0010-000&quantity=100&wiretype=SAVSS0.85B&wirelength=0.455&terminalA=916189-000%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20&sealA=255146-000%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20&terminalB=916876-010&sealB=255146-000

This is what i want:这就是我要的:

stockcode=1ECN0010-000&quantity=100&wiretype=SAVSS0.85B&wirelength=0.455&terminalA=916189-000&sealA=255146-000&terminalB=916876-010&sealB=255146-000 

Just decode the url, use the replace function to eliminate the whitespaces and then encode it again.只需解码 url,使用替换功能消除空格,然后再次编码。

function removeSpaces(url) {
  return encodeURIComponent(decodeURIComponent(url).replace(/\s+/g, ''));
}

JS replace-Function to eliminate whitespaces: How to remove spaces from a string using JavaScript? JS 替换功能以消除空格: 如何使用 JavaScript 从字符串中删除空格? Javascript decode and encode URI: http://www.w3schools.com/jsref/jsref_decodeuri.asp Javascript 解码和编码 URI: http : //www.w3schools.com/jsref/jsref_decodeuri.asp

如果theString有您的原始字符串,请尝试:

theString = theString.replace(/%20/g, "")

you can simply replace the substring you want ("%20") with nothing (or "").你可以简单地用空(或“”)替换你想要的子字符串(“%20”)。 Try something like this:尝试这样的事情:

var str = "stockcode=1ECN0010-000&quantity=100&wiretype=SAVSS0.85B&wirelength=0.455&terminalA=916189-000%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20&sealA=255146-000%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20&terminalB=916876-010&sealB=255146-000"
var res = str.replace(/%20/g, "");

Hope it helps.希望能帮助到你。


The idea is just the 1%, now go make the 99% !这个想法只是 1%,现在去创造 99%!

This Might Help you:这可能会帮助您:

function doIt(){
var url="stockcode=1ECN0010-000&quantity=100&wiretype=SAVSS0.85B&wirelength=0.455&terminalA=916189-000%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20&sealA=255146-000%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20&terminalB=916876-010&sealB=255146-000";
var str=url.toString();
str=str.split("%20").join("");
return str;
}

Here is Working Fiddle .这是工作小提琴

try this function :试试这个功能:

function Replace(str) {
  var del = new RegExp('%20');
  str.match(del);
}

You will need to treat the URL as string and remove the whitespace inside that string using your programming language.您需要将 URL 视为字符串并使用您的编程语言删除该字符串中的空格。

For example, if you use JavaScript to remove the whitespace from the URL you can do this:例如,如果您使用 JavaScript 从 URL 中删除空格,您可以这样做:

let string = `https://example.com/    ?key=value`;
string.replace(/\s+/g, "");

This will give you: https://example.com/?key=value;这会给你: https://example.com/?key=value;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM