aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes string.replace() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "string.replace()" Watch "string.replace()" New topic
Author

string.replace()

Viralraj Upadhyay
Greenhorn

Joined: Nov 10, 2006
Posts: 22
This is related to javascript. I have a string with a comma separated set of values.

eg:-

1,1,1,1,1@1.com,1
2,2,2,2,2@1.com,2
3,3,3,3,3@1.com,3
4,4,4,4,4@1.com,4

These are entered in a text box.After each line I would like to replace the white space with a " , " (comma).These values are stored in a string.

When I do string.replace("\r\n",","); only the white space after the first line gets replaced by comma. I want to replace the white spaces after each line with a comma. How should i go about it?
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10210
    
166

Try this:


[My Blog] [JavaRanch Journal]
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

hi Jaikiran,

I tried your code on IE 7, with error less but different output.

and also can't figure out why is producing a different output on IE7.


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10210
    
166

Hmmm... Did not try it on IE7 (dont have version on my machine). What's the difference that you see on IE7?
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

the output is same what Viralraj wrote above.

only the white space after the first line gets replaced by comma but others are not.

but its fine in Mozilla FireFox.
Viralraj Upadhyay
Greenhorn

Joined: Nov 10, 2006
Posts: 22
str.replace("\n",",","\g");

did not work for me
Scott Escue
Ranch Hand

Joined: Jan 20, 2005
Posts: 34
Viralraj,

The replace function expects the first argument (the regular expression) to be passed without surrounding quotes. I know it looks odd, but that's how the function works.

The snippet below worked for me:
Viralraj Upadhyay
Greenhorn

Joined: Nov 10, 2006
Posts: 22
Scott,

Thanks, it worked for me too. can you please explain how this regular expression works?
Scott Escue
Ranch Hand

Joined: Jan 20, 2005
Posts: 34
Viralraj,

The format for the regular expression argument is "/regular expression/expression modifiers". So the base regular expression /\r\n/ is the same one you used to begin with. By default, the regular expression only selects the first occurrence of the specified pattern. To specify that you want to select, and eventually replace, all occurrences of the pattern you need to add the "g" (for global) expression modifier, /\r\n/g. So now when this gets passed to the replace function, all instances of "\r\n" will be replaced with a comma. Instead of just the first one.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: string.replace()