Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

string.replace()

 
Viralraj Upadhyay
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
str.replace("\n",",","\g");

did not work for me
 
Scott Escue
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott,

Thanks, it worked for me too. can you please explain how this regular expression works?
 
Scott Escue
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic