Printable Version of Topic

Click here to view this topic in its original format

HTMLHelp Forums _ Client-side Scripting _ Issues with Javascript Replace

Posted by: kspn Apr 27 2009, 01:00 AM

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. blink.gif

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. blink.gif
Each line of code run independently seems to work as expected, but when combined it fails.

Any assistance would be appreciated.

Posted by: kspn Apr 27 2009, 01:55 AM

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, '\. ')

blush.gif

Posted by: Christian J Apr 27 2009, 08:27 AM

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.




Posted by: Christian J Apr 27 2009, 01:12 PM

QUOTE(Christian J @ Apr 27 2009, 03:27 PM) *

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".

Posted by: kspn Apr 27 2009, 08:39 PM

QUOTE(Christian J @ Apr 28 2009, 04:12 AM) *

QUOTE(Christian J @ Apr 27 2009, 03:27 PM) *

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, '\. ')

Posted by: Christian J Apr 28 2009, 05:02 AM

I'm not good at regex, but I think this:

QUOTE
CODE
ProductOI = ProductOI.replace(/\n\r/g, '\. ')

should be the other way around to catch Windows CR+LF, like this:

CODE
ProductOI = ProductOI.replace(/\r\n/g, '\. ');


Also, these don't remove all occurences:

QUOTE
CODE
//Replace '.. ' with '. '
ProductOI = ProductOI.replace(/\.\. /g, '\. ')
//Replace '. . ' with '. '
ProductOI = ProductOI.replace(/\. \. /g, '\. ')

(is it because each additional pair will be reduced to an additional single, not one in total?). See if these work better:

CODE

// One or more . is replaced with a single
ProductOI = ProductOI.replace(/(\.)+/g, '\. ');
ProductOI = ProductOI.replace(/(\. )+/g, '\. ');

Still if the user inserts blank spaces between newlines it will prevent the removal of duplicates. Not sure how to remove spaces safely since the \s special character also catches newlines.

I'm not sure if the escaping backslash is necessary when using quotes and/or parenthese, but it doesn't seem to harm anything either. Only tested on Windows.

BTW if you're going to use this is public you may need to re-check at the server-side as well, in case users have disabled JS.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)