This is a post relating to the KeyPress issue of an earlier post.
I would like now to look into the *middle* of a string of input chars,
and change x (e.g., 10) successive space chars to y (e.g., 2) space chars.
Also, I'd like to do the same sort of thing for newline chars .
In both case I'd like to be able to modify the 'x' and 'y' parameters.
Altho I'm a brand newbie on RegExp stuff, I'm guessing that a statement
like the following may work:
*********************************************
var badtext = "a bunch of chars with perhaps many *internal* successive
spaces and/or many successive *internal* newline/crlf chars"
var goodtext = badtext.replace(/somepatternhere/,"")
*********************************************
Would someone suggest a proper pattern for my testing please ?
Thank you.
--
Mel Smith
z='aaaa bbbb c';
n=z.replace(/ {10,}/,'xx');
alert(z);
//replaces once exact 10 subsequent blanks, optional parameter after
comma allows definition of range, by xx
z='aaaa bbbb c';
n=z.replace(/ {10,}/,'xx');
alert(z);
//replaces once exact 10 subsequent blanks, optional parameter after
comma allows definition of range, by xx
Typo:
I'll try your suggestion this afternoon
Thank you !
-Mel Smith
I'm also a newbie!
I am not sure exactly what you are needing. Do you want to replace multiple
space or newline characters with 2 spaces?
Maybe something like this (untested!)
var goodtext = badtext.replace(/\s{3,}/,' ')
This will replace 3 or more whitespace characters with 2 spaces.
MG
>
> I'm also a newbie!
>
> I am not sure exactly what you are needing. Do you want to replace
> multiple space or newline characters with 2 spaces?
>
> Maybe something like this (untested!)
> var goodtext = badtext.replace(/\s{3,}/,' ')
>
> This will replace 3 or more whitespace characters with 2 spaces.
Hi MG:
Yes, I wish to replace a stream of spaces to a set number of spaces --
say replace any ten consecutive spaces with two spaces. *Then*, I wish to
repeat the procedure in case I have a multitude of spaces. That is, I wish
to iterate the procedure to replace any consecutive amount of spaces to just
two spaces.
Also, I wish to do a similar thing with many CR-LF sequences.
I've been working this morning and afternoon, and I'm now experimenting
with the following, but have had limited success in understanding what's
going on:
I'm taking "stabs-in-the-dark", and I'm getting dangerous !
*************************************************************************
evtdetails = evtdetails.replace(/\x0D\x0A\x0D\x0A\x0D\x0A+/gi,"\x0D\x0A") ;
//change three crlfs to 2 crlfs
evtdetails = evtdetails.replace(/ +/gi," "); // five spaces converted
to three spaces
*************************************************************************
Thanks for the suggestion !
-Mel
It does not, it replaces "at least" 10 spaces.
Exactly 10 spaces would be {10} without the comma.
If you want to repeat this action over the whole string,
add the global g, as in /.../g
> optional parameter after
> comma allows definition of range, by xx
A blank is regexed by \s, and is not only a space,
ia also a tab, a new line and more.
If you want to replace alle series of blanks by a space do this:
var result = source.replace(/\s+/g,' ');
If you want only series of more than one blank with a space, try:
var result = source.replace(/\s\s+/g,' ');
If you want to replace those two or more with the first blank,
so if that is a tab, keep the tab:
var result = source.replace(/(\s)\s+/g,'$1');
[in some non-browser applications replace @1 by \1]
===========================
Often one wants to replace all series of blanks with a space and also
trim the outer spaces:
<script type='text/javascript'>
var source = ' a \t \n b ';
var result = source.replace(/\s+/g,' ').replace(/^\s+|\s+$/g,'');
alert('>' + result + '<');
alert('string length: ' + result.length);
</script>
The alerted result is ">a b<"
The string length is 3.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
> Often one wants to replace all series of blanks with a space and also
> trim the outer spaces:
>
> <script type='text/javascript'>
> var source = ' a \t \n b ';
> var result = source.replace(/\s+/g,' ').replace(/^\s+|\s+$/g,'');
> alert('>' + result +'<');
> alert('string length: ' + result.length);
> </script>
>
> The alerted result is ">a b<"
> The string length is 3.
How would you modify your regexp to also replace a non-space blank,
that's not in a series of blanks, with a space?
Andrew Poulos
Evertjan:
Thank you for the detailed explanation. I'll try to implement it this
afternoon
-Mel Smith
Well, I've tried everything till my brain has bust out thru my eyeballs
:(
Here's what works for me :
****************************************************
var evtdetails = frmeditevent.eventdetails.value // get the string of up to
254 chars
evtdetails = alltrim(evtdetails) ; // trims leading and trailing invisible
chars
var detailcopy = evtdetails // get a copy of the 'trimmed' string
// Now try replacing *internal* unnecessary chars
for (var i=0;i<=100;i++) {
evtdetails =
evtdetails.replace(/\x0D\x0A\x0D\x0A\x0D\x0A\x0D\x0A+/g,"\x0D\x0A\x0D\x0A\x0D\x0A")
; //change 4 or more crlfs to 3 crlfs
evtdetails = evtdetails.replace(/ +/g," "); // change 4 or more
spaces to 2 spaces
if (detailcopy.length === evtdetails.length) break ; // if no changes,
then break out of this loop
detailcopy = evtdetails ; // gwet a copy for next iteration
}
********************************************************
After re-reading the suggestions here, I know that I screwed up somewhere,
but I'm too tired to try anything else for now
Thanks you all for your suggestions. I'll read them again later.
I haven't used hex characters in a regular expression, but I see it as
follows:
The + will only apply to the final \x0A.
So your expression won't work as expected.
Try (untested):
....replace(/(\x0D\x0A){4,}/g,....
That should do 4 or more crlfs
> evtdetails = evtdetails.replace(/ +/g," "); // change 4 or more
> spaces to 2 spaces
same again.
Try:
....replace(/ {4,}/g....
(There is a single space between the / and the {
That should do 4 or more spaces.
As I said I am also a newbie, so if I am wrong I hope someone will correct
me.
MG
> I haven't used hex characters in a regular expression, but I see it as
> follows:
> The + will only apply to the final \x0A.
> So your expression won't work as expected.
MG:
But it *does* seem to work ??
> Try (untested):
> ....replace(/(\x0D\x0A){4,}/g,....
> That should do 4 or more crlfs
>
>
>> evtdetails = evtdetails.replace(/ +/g," "); // change 4 or more
>> spaces to 2 spaces
>
> same again.
> Try:
> ....replace(/ {4,}/g....
> (There is a single space between the / and the {
> That should do 4 or more spaces.
>
> As I said I am also a newbie, so if I am wrong I hope someone will correct
> me.
MG:
AS noted above, it does seem to work. However, I don't know why --- or
how to make it shorter.
But I *will* try out your {4,} idea too
Thanks,
-Mel
IIUC, let `b' be a "non-space blank" and not be matched by /\s/:
/* "a b a c" */
var result = ' a \t \n b abc '
.replace(/^\s+|\s+$/g, '')
.replace(/\s+|b(?!\s)/g, ' ');
One issue here is backwards-compatibility, see <http://PointedEars.de/
es-matrix#features> (last modified: 2010-06-17T10:50:45+00:00).
Switching the replacing order is probably more efficient anyway.
PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
I think there is no need of loop, the flag 'g' does it
> evtdetails =
> evtdetails.replace(/\x0D\x0A\x0D\x0A\x0D\x0A\x0D\x0A+/g,"\x0D\x0A\x0D\x0A\x0D\x0A")
x0a = LF = \n
x0d = CR = \r
javascript:
evtdetails = 'a\x0D\x0A\x0D\x0A\x0D\x0A\x0D\x0A\x0D\x0A\x0D\x0A\x0D\x0A
b\n\n\n c d';
alert(evtdetails);
evtdetails = evtdetails.replace(/[\n\r]{4,}/g,'\x0D\x0A');
alert(evtdetails);
evtdetails = evtdetails.replace(/ {4,}/g,' ');
alert(evtdetails);
alert(evtdetails.replace(/ /g,'.'));
> ; //change 4 or more crlfs to 3 crlfs
> evtdetails = evtdetails.replace(/ +/g," "); // change 4 or more
> spaces to 2 spaces
> if (detailcopy.length === evtdetails.length) break ; // if no changes,
> then break out of this loop
> detailcopy = evtdetails ; // gwet a copy for next iteration
> }
>
> ********************************************************
>
> After re-reading the suggestions here, I know that I screwed up somewhere,
> but I'm too tired to try anything else for now
all made examples :
<http://cjoint.com/?jgovp1r2qD>
not tested with IE ...
--
Stéphane Moriaux avec/with iMac-intel
I'll try it again tomorrow. Sigh .....
Thank you.
-Mel
> SAM:
>
> I'll try it again tomorrow. Sigh .....
I think it is time you try to learn Regex itself
and use our examples only to understand regex.
Using the examples for implementation without understanding
would be very dangerous indeed.
So scrutinize the specs of MS "script56" and/or Mozilla.
especially since I do not really understand what Mel wants to ... :-(
(with those multiple returns and spaces to keep, once by 3, once by 4)
> So scrutinize the specs of MS "script56" and/or Mozilla.
<https://developer.mozilla.org/en-US/search?q=regexp>
<http://evolt.org/node/36435/>
<http://www.google.com/search?q=javascript+regexp+tester>
In french :
- NG about RegExp : news:fr.comp.lang.regexp
- RegExp tester :
<http://toutjavascript.com/service/regexp.php>
<http://stephane.moriaux.pagesperso-orange.fr/truc/js_regexp_testeur_om>