Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
61
rated 0 times [  62] [ 1]  / answers: 1 / hits: 39555  / 12 Years ago, wed, june 13, 2012, 12:00:00

I have not had to do something like this in the past and am wondering if it is indeed possible. I am allowing multiple code numbers to be added in an so long as they are delimited by commas. What I am wanting to do is upon the user clicking on the okay button that a showing the numbers entered will show them one on top of each other with a delete button next to them. That part is easy...the hard part is getting the comma stripped out and the new line placed in its stead.



Are there any examples or samples that anyone can point me too?


More From » jquery

 Answers
17

You'd use String#replace with a regular expression using the g flag (global) for the search part, and a replacement string of your choosing (from your question, I'm not sure whether you want <br> — e.g., an HTML line break — or n which really is a newline [but remember newlines are treated like spaces in HTML]). E.g.:



var numbers = 1,2,3,4,5,6;
numbers = numbers.replace(/,/g, '<br>'); // Or n, depending on your needs


Or if you want to allow for spaces, you'd put optional spaces either side of the comma in the regex:



var numbers = 1,2,3,4,5,6;
numbers = numbers.replace(/ *, */g, '<br>'); // Or n, depending on your needs

[#84935] Tuesday, June 12, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dallasb

Total Points: 657
Total Questions: 98
Total Answers: 97

Location: Luxembourg
Member since Tue, Jan 25, 2022
2 Years ago
;