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

javascript - How to remove empty spaces in the url - Stack Overflow

programmeradmin0浏览0评论

Hi how can i remove empty space in the URL using javascript: here how it looks like

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 

Hi how can i remove empty space in the URL using javascript: here how it looks like

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 
Share Improve this question asked May 16, 2014 at 13:16 billah77billah77 1941 gold badge1 silver badge13 bronze badges 2
  • url = url.replace(/%20/g, ''); – Aamir Afridi Commented May 16, 2014 at 13:21
  • 1 I don't think this is what you want... I think you need to trim the value before you dynamically (I assume) build this URL. – Ayman Safadi Commented May 16, 2014 at 13:21
Add a ment  | 

6 Answers 6

Reset to default 1

Just decode the url, use the replace function to eliminate the whitespaces and then encode it again.

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? Javascript decode and encode URI: http://www.w3schools./jsref/jsref_decodeuri.asp

If theString has your original string, try:

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

you can simply replace the substring you want ("%20") with nothing (or ""). 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% !

try this function :

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

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.

You will need to treat the URL as string and remove the whitespace inside that string using your programming language.

For example, if you use JavaScript to remove the whitespace from the URL you can do this:

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

This will give you: https://example./?key=value;

发布评论

评论列表(0)

  1. 暂无评论