Issues with Javascript Replace |
Issues with Javascript Replace |
kspn |
Apr 27 2009, 01:00 AM
Post
#1
|
Group: Members Posts: 3 Joined: 27-April 09 Member No.: 8,448 |
Hi,
I am trying to get the values form a Textarea, replace any returns with '. ' and also ensure that there are no duplicates (ie) '.. ' or '. . ' So far I am having weird results. CODE ProductOI = document.getElementById('ProductOI').value; ProductOI = ProductOI.replace(/\n/g, '\. '); ProductOI = ProductOI.replace(/\.\. /g, '\. '); ProductOI = ProductOI.replace(/\. \. /g, '\. '); The 'NewLine' code is working but the other two for some reason are not working as I am expecting them too. Each line of code run independently seems to work as expected, but when combined it fails. Any assistance would be appreciated. |
kspn |
Apr 27 2009, 01:55 AM
Post
#2
|
Group: Members Posts: 3 Joined: 27-April 09 Member No.: 8,448 |
Solved!!
Windows was adding \r to it as well, so the following code works. CODE ProductOI = ProductOI.replace(/\n/g, '\. ') ProductOI = ProductOI.replace(/\r/g, '') ProductOI = ProductOI.replace(/\.\. /g, '\. ') ProductOI = ProductOI.replace(/\. \. /g, '\. ') |
Christian J |
Apr 27 2009, 08:27 AM
Post
#3
|
. Group: WDG Moderators Posts: 9,653 Joined: 10-August 06 Member No.: 7 |
Multiple newlines like the following:
CODE <textarea>a b</textarea> seem to result in something like "a. . b". Also, IIRC Mac OS use only \r for newlines, so the script above would not produce any period signs then. |
Christian J |
Apr 27 2009, 01:12 PM
Post
#4
|
. Group: WDG Moderators Posts: 9,653 Joined: 10-August 06 Member No.: 7 |
Also, IIRC Mac OS use only \r for newlines, so the script above would not produce any period signs then. According to http://en.wikipedia.org/wiki/Newline#Representations CR (which corresponds with \r) was used by "Commodore machines, Apple II family, Mac OS up to version 9 and OS-9". |
kspn |
Apr 27 2009, 08:39 PM
Post
#5
|
Group: Members Posts: 3 Joined: 27-April 09 Member No.: 8,448 |
Also, IIRC Mac OS use only \r for newlines, so the script above would not produce any period signs then. According to http://en.wikipedia.org/wiki/Newline#Representations CR (which corresponds with \r) was used by "Commodore machines, Apple II family, Mac OS up to version 9 and OS-9". I have updated the code to the following and it appears to work as I am expecting. CODE //Replace NewLine/Line Feed with '. ' ProductOI = ProductOI.replace(/\n\r/g, '\. ') //Replace NewLines with '. ' ProductOI = ProductOI.replace(/\n/g, '\. ') //Remove Line Feed with '. ' ProductOI = ProductOI.replace(/\r/g, '\. ') //Replace '.. ' with '. ' ProductOI = ProductOI.replace(/\.\. /g, '\. ') //Replace '. . ' with '. ' ProductOI = ProductOI.replace(/\. \. /g, '\. ') |
Lo-Fi Version | Time is now: 19th April 2024 - 05:28 PM |