Question:

Javascript question. REPLACE METHOD?

by  |  earlier

0 LIKES UnLike

I want to replace " " with "".

In other words, I want to replace SPACES with NOTHING

1 2 3 4 5 will be turned to 12345

I am currently using this:

var str="1 2 3 4 5";

str2=(str.replace(//g, "");

it game me error. how to fix it?

alert(str2);

 Tags:

   Report

2 ANSWERS


  1. function removeSpaces(string) {

    var tstring = "";

    string = '' + string;

    splitstring = string.split(" ");

    for(i = 0; i < splitstring.length; i++)

    tstring += splitstring[i];

    return tstring;

    }


  2. 1: You have an unneeded '(' after the = sign.

    2: You need to include \s in the regular expression .replace(/\s/g,"") to match a space.

    Other than that, your strategy works fine

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions