Useful notes & manuals
from the
world wide web
Register now!
Space in Url and JavaScript trim and replace function JavaScript
Solution from: Created by tlw on 29.06.2022 @ 07:19

In a recent (also age-old) mapping application for our website (, I encountered 2 problems: 1. how to trim the leading and trailing space in the url's parameter; 2. how to include space in an url

(I must have been living in rocks that I never realized that it was illegal to include flat space in url, and the right way to do it is to replace ALL spaces with %20.)

There is no built in trim() function in Javascript, fortunately there are many people who has nothing better to do than write code and present it online. So here is a prefect trim function :

string=string.replace(/^s+|s+$/g, '');

Cever use of regular expressions.

As for replacing all spaces inside a string with %20, here it goes


trim   replace   string   url   space   %20
Created by THE LOST WEB © 2009-2012