Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Spaces and special characters

Status
Not open for further replies.

pmcmicha

Technical User
May 25, 2000
353
I am attempting to remove spaces and other special characters from a variable reference. This reference is passed from an array. This array is based into a function to build a table. That part works without a problem, but I would like to be able to create an <a href="" without the real variable name, but with one that does not have the spaces or other special characters.

Here is what I came up with, but it goes into an infinite loop.

Code:
var name = this.data[i*this.columns+j];
while(name.indexOf(" ") != -1) {
        var n_name = name.substring(0,name.indexOf(" ")) + name.substring(name.indexOf(" ")+1,name.length);
}
doc.write("<td><a href=\""+n_name+"\">");
...
(If you cannot have a space between the '+' sign and whatnot, in my code there is not a space, but to get this to appear in a more readable format, I added a space.)

While this looks right, I know that I am missing something from this idea. I am also quite new to this language and any help is appreciated.

Thanks in advance.
 
Code:
var name = this.data[i*this.columns+j];
var n_name = name.split(" ").join("");
It uses 2 JavaScript methods:
- split: splits a string into an array based on the given seperator (" ")
- join: joins elements of an array together to form a string, putting the given seperator ("") between each element.

I'm sure there are other ways!

<marc>
 
You could also use the replace method with a regular expression:
Code:
var re = / /g;
n_name = name.replace(re,"");
The advantage of this method is that you can include other characters in the regular expression, and replace them all at once. Before you start putting special characters into the regular expression, check the MSDN JScript Language Reference, Regular Expression Syntax. Many special characters have special meaning within a regular expression.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top