PDA

View Full Version : regular expresions help


scroots
05-04-2003, 05:28 PM
i have
var str = document.form.text.value.replace(/(From:)+(end)/g,'');

how can i use it to the effect so that instead of replacing From:end it replaces From: and everything in between it (between From: and end), including new lines etc. and end.

so
From:tgfg
fgfgfd
fgfshgjhg
gjyhjy
ghghjh
end
jogging

would become jogging.

scroots

liorean
05-04-2003, 06:13 PM
Ok, if we stay in the MBP (Multilingual Base Plane, or the first 2^16 entries in the Unicode standard), we can do this:
sCutOut=sOriginal.replace(/from:[\u0001-\uffff]+?end/, '');It might not work in all browsers, though, so what if we try to do it the ASCII way instead?sCutOut=sOriginal.replace(/from:[\x01-\xff]+end/, '');This will work in more browsers, but have two problem - it'll end at the last uccurence of end after from:, not the first, and it'll not catch as many different characters.

It should be enough for what you need, though.

scroots
05-04-2003, 06:22 PM
thanks so far, the only thing is that it doesn't seem to be able to cope wtih new lines.
any ideas?

scroots

Philip M
05-04-2003, 06:54 PM
I suggest:-

sCutOut=sOriginal.replace(/^from:[\x01-\xff]+end$/, '');

^ and $ match the beginning and the end of a string (not lines in a string). I have not tried it though.

Another approach:-

sCutOut=sOriginal.replace(/(^from)(.*|\n)(end)/gi, '');

This works:-

<SCRIPT language="JavaScript">
sOriginal = "fromhereto eternity" +
"somethingelse" +
"anotherthingend"

sCutOut=sOriginal.replace(/(^from)(.*|\n)(end)/gi, 'replaced');

alert (sCutOut)

</SCRIPT>

The contents of each line must be enclosed in quotes

liorean
05-04-2003, 07:13 PM
scroots:
Hmm, it should work with newlines...
Try [^\x00] instead of that interval, though. It might well work.
(NULL isn't allowed anyways, so we can easily ignore it.)

Philip M:
...and what if from: isn't the first character sequence?

The first one you entered wouldn't work, it only takes into account those times end is the last part of the string, and is expressedly not wanted behavior since he wants just that part to remain. (Which you just edited so it works)

The (.|\n)* approach should work for newlines (not the moval of the * from inside to outside the parens).... Your code only took into account either one newline or an arbitrary amount of non-newline characters.

Philip M
05-04-2003, 07:24 PM
Well, I understood that "from" were the first characters in the string.

You proposed:-

sCutOut=sOriginal.replace(/from:[\x01-\xff]+end/, '');

As I said above (but in a mod so out of sequence):

This works:-

<SCRIPT language="JavaScript">
sOriginal = "from here to eternity" +
"something else" +
"another thing" +
"end" +
"jogging"

sCutOut=sOriginal.replace(/(^from)(.|\n)*(end)/gi, 'replaced ');

alert (sCutOut)

</SCRIPT>

The contents of each line must be enclosed in quotes

sCutOut contains replaced jogging

scroots
05-04-2003, 09:36 PM
the following works:

sCutOut=sOriginal.replace(/from:[\x01-\xff]+end/, '');


thanks for all your hard work, you will see this code crop up somewhere else soon.

thanks
scroots