...

View Full Version : Replace() dilemma



Mr J
12-21-2003, 09:03 PM
Hi everybody

I am wanting to replace the Break tags "<br>" in a string.
All is well with a single break tag but if I have two together "<br><br>" then only one is replaced.

I am using the following code

<script>
function mailme(){
txt="This<br>is<br><br>a<br>test"
pattern = /\<br>\b/ig;
newString = txt.replace(pattern,"%0D%0A");
location="mailto:yourname@youraddress.com?Body="+newString
}
</script>

<a href="#null" onclick="mailme()">Mail Me</a>

What I should get is an empty line between "is" and "a"

This
is

a
test

What I actually get when two br tags are together is

This
is<br>
a
test


note the break tag

I am not really up on reg expressions so I need a bit of help here

Thank you

nolachrymose
12-21-2003, 09:15 PM
The problem is that the regular expression looks for a <br> tag with a word boundary after it, so if there are two together only one will be removed. Try this:


var pattern = /\<br\>/gi;

Hope that helps!

Happy coding! :)

Mr J
12-21-2003, 09:26 PM
nolachrymose

Thank you very much for your help it certainly did the trick.

I must find the time to learn more about regular expressions but for now, once again, thank you

Mr J
12-21-2003, 09:42 PM
nolachrymose

I also found that this worked

pattern = /\<br>/ig;

when you mentioned the word boundary I thought "hmm....is that what the \b is for" so I took out the \b from mine and thats how I found it worked, unless it is still not the right way to code it

but once again thanks



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum