47

I am trying get the string in the following URL to display on my webpage.

http://example.com?ks4day=Friday+September+13th

EDIT: The date in the URL will change from person to person as it's merged in by my CRM program.

I can get it to display on my webpage using the code below, the problem is the plus signs (+) come through as well.

eg. Friday+September+13th

What I need it to do is replace the plus signs (+) with spaces so it looks like this:

eg. Friday September 13th

I'm new to this so I'm having some trouble working it out.

Any help would be appreciated.

This is the code i'm using in a .js file

      function qs(search_for) {
    var query = window.location.search.substring(1);
    var parms = query.split('&');
    for (var i=0; i<parms.length; i++) {
        var pos = parms[i].indexOf('=');
        if (pos > 0  && search_for == parms[i].substring(0,pos)) {
            return parms[i].substring(pos+1);;
        }
    }
    return "";
}

This is the code i'm using on my webpage to make it display

     <script type="text/javascript">document.write(qs("ks4day"));</script>
6
  • 1
    str.replace('+', ' '); would replace + with a space in the provided string. Commented Sep 10, 2013 at 11:22
  • 7
    decodeURIComponent("Is+it+friday+the+13th%3F").replace(/\+/g, " ") Commented Sep 10, 2013 at 11:31
  • Possible duplicate of Replacing all plus symbols in JavaScript String Commented Sep 10, 2013 at 11:35
  • 16
    @SalmanA - Other way around: decodeURIComponent("1+%2B+2+%3D+3".replace(/\+/g, '%20')) otherwise you decode the "%2B" incorrectly Commented Sep 10, 2013 at 11:53
  • 3
    @user2764485 You should do the .replace(/\+/g, " ") FIRST, not last, as @cloudfeet described above. Otherwise a properly-encoded plus sign will be replaced with a space, incorrectly! Commented Jun 1, 2016 at 16:15

5 Answers 5

143

Although Bibhu's answer will work for this one case, you'll need to add decodeURIComponent if you have encoded characters in your URI string. You also want to make sure you do the replace before the decode in case you have a legitimate + in your URI string (as %2B).

I believe this is the best general way to do it:

var x = qs("ks4day");        // 'Friday+September+13th'
x = x.replace(/\+/g, '%20'); // 'Friday%20September%2013th'
x = decodeURIComponent(x);   // 'Friday September 13th'

Here's an example of when it might be useful:

var x = '1+%2B+1+%3D+2'; 
x = x.replace(/\+/g, '%20'); // '1%20%2B%201%20%3D%202'
x = decodeURIComponent(x);   // '1 + 1 = 2'
Sign up to request clarification or add additional context in comments.

3 Comments

I found this question when looking for help specifically with plus signs. Even though I was already decoding with Node's unescape function, that function doesn't decode plus signs. This answer is the helpful one to me, and probably to most people who end up here.
Thank you! I was not aware of the decodeURIComponent() function.
Perfectly working for me. It is avoiding of following changes when you are using ajax to get input values. 1). Space into plus sign. 2). plus sign into %20
13

Parsing strings using regex is often prone to so many errors. Thankfully all modern browsers provide URLSearchParams to handle params from url strings in a proper way:

var params = new URLSearchParams(window.location.search);
var value = params.get('ks4day');
// "Friday September 13th"

Ps: There is also a good polyfill for old browsers.

2 Comments

this is definitely the best answer, the other ones are at least outdated
Using native implementation is always best. For sure the best answer as of 2020.
12

You can use replace() for this purpose

var dateString = 'Friday+September+13th';
var s = dateString .replace(/\+/g, ' ');

Comments

0

Have you tried https://www.npmjs.com/package/querystring ?

import { parse } from 'querystring'; 
parse('ks4day=Friday+September+13th')

returns

{ 'ks4day': 'Friday September 13th' }

Assuming you are using something like Webpack that knows how to process import statements

1 Comment

Simple and best .
-2

If that's what you are doing, the plus sign will not be the only one that is going to give you a hard time. The apostrophe ('), equals (=), plus (+) and basically anything not in the permitted URL characters (see Percent-encoding @ Wikipedia) is going to get escaped.

You are most likely looking for the decodeURIComponent function.

7 Comments

Also check documentation on decodeURI(): w3schools.com/jsref/jsref_decodeuri.asp
@SalmanA - You should replace "+" before decodeURIComponent, not after - otherwise you'll incorrectly decode "%2B" to " ".
Woo Hoo. Got it working Using the decodeURIComponent. eg. <script type="text/javascript">document.write(decodeURIComponent(qs("ks4day")).replace(/‌​\+/g, " "));</script>
What if you have a phone number with country code in that string? Replace is the worst possible option. decode doesn't remove pluses.
@user2764485, You can't do a replace after decoding because you'll be decoding "%2b" to " ". And that is wrong.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.