PDA

View Full Version : Add Single space Padding to....


senshi
03-24-2005, 11:01 PM
IM wanting to process a textarea to insert at the punctuation characters like quotes and hyphens etc with a single whitespace character padding.

Not sure how you do this with regular expressions and the various functions associated with the regular expression, any ideas on how I go about this?

IKLOP
03-24-2005, 11:30 PM
I don't think you would have to deal with regular expressions in this case:var content = document.getElementById("textareaname").value;
content.replace("\""," \" ");
content.replace("-"," - ");
that would add a space around quotes and hyphens.

senshi
03-25-2005, 04:22 AM
OK, thanks.

I was assuming that their was a regular expression for punctuation !"£$%&*()_-+=[]{}:;@'~#<>,.?/\|.

would I be correct in assuming that if I put that string into a regular expression that the expression could be used to pad the characters found with a space.

Existing whitespace is ok as it is and needs no change.

So would this do it..

rexp=/!"£$%&*()_-+=[]{}:;@'~#<>,.?/\|/ig;
content.replace(rexp," "+rexp+" ");

or is that being too simplistic and what about characters that form the end of a word, they only need to be padded on the left of the character as in commas, semi colons and periods and others like quotes, hyphens & that require padding on both sides.

example of what IM loking to do....

if the folowing was in the text area...

if(!arguments.length){

to turn it into...

if ( ! arguments . length ) {

or if a passage of thext was in the textarea...

Hello! Nice isn't it, how are you today?

to...

Hello ! Nice isn ' t it , how are you today ?


Any help is appreciated, have tried finding out this information myself but RegExp's are at the best of time confusing.