...

View Full Version : Regular Expression to Replace <br /> tag



JohnKrutsch
07-08-2004, 05:51 PM
Ok, regular expressions are not my strong suite but I have always been able to figure out what I need in the past. I just can't get my head around this one:

msg=msg.replace(all instances of <br /> found with the <xmp></xmp> tag,"\n");

I can't figure out how to change just that one area as I need the <br /> tags to remain everywhere else.

jbot
07-08-2004, 06:19 PM
couldn't you just loop round the childNodes of the code block, checking the nodeValue, and if it's a breakline, then replacing it with the newline char?

Catman
07-08-2004, 06:56 PM
Probably the long way around, but how about something like this:



splitString = msg.split("<xmp>"); // xmp contents plus following
// items in splitString[1]
xmpString = splitString[1].split("</xmp>"); // xmp contents in xmpString[0]
newString = xmpString[0].replace(); // insert appropriate regular expression
msg = splitString[0] + "<xmp>" + newString + "</xmp>" + xmpString[1];
// rebuild the message with the altered xmp contents
This, of course, assumes only one xmp element per message.

neofibril
07-09-2004, 02:26 AM
<body>
<script type="text/javascript">
function repl(txt)
{
var val = txt.value,
rx = /<xmp>.+<\/xmp>/g,
ar = val.match(rx),
i = -1;
alert(val.replace(rx, function(){return ar[++i].replace(/<br \/>+/g, "\n")}));
}
</script>
<form style="text-align:center">
<textarea name="txt" cols="50" rows="25">
<div>
<br />
<xmp><br />blah<br />blah<br /></xmp>
<br />
</div>
<br />
<xmp><br />foo<br />foo<br /></xmp>
</textarea>
<br>
<input type="button" value="test" onclick="repl(txt)">
<form>
</body>
</html>



The increment isn't really necessary, huh. :rolleyes:

function repl(txt)
{
var val = txt.value,
rx = /<xmp>.+<\/xmp>/g,
ar = val.match(rx);
alert(val.replace(rx, function(){return ar.shift().replace(/<br \/>+/g, "\n")}));
}

Backreferences might be the key to a real solution.

JohnKrutsch
07-09-2004, 05:18 AM
Thanks for that. It got me to where I needed to be. It did not seem to grab multiple lines of text though. Lets say the code looked like:



<xmp>
p {
font-size: 12px;
}

.p1 {
color: white
}

.p2 {
color: yellow
}

.p3 {
color: #87ceeb;
}
</xmp>


the .+ was not sufficient, but I got it to work with this regex:

/<xmp>([\w|\t|\r|\W]*?)<\/xmp>/g

Your bit with the anonymous function was the last piece in the puzzle. It worked out wonderfully.

neofibril
07-09-2004, 05:41 AM
Yeah, I'm no pro with the syntax; originally tried [\s\S]*, but that did too much, so...

Good topic; I'll use the idea for file system stuff, no doubt.

Willy Duitt
07-09-2004, 05:46 AM
Can we see a working example of how you implemented the expression. I was not sure if were converting the contents of the <xmp> .....</xmp> tags which were on a page or were sending them as a string on a form or something....

BTW: I had went in thru the innerHTML but was not sure if that was what you wanted, but FWIW, here it is for the archives....



<script>
function xmp(){
var xmp = document.getElementsByTagName('xmp');
alert(xmp[0].innerHTML);
alert(xmp[0].innerHTML.replace(/<br>/g,'\n'));
document.forms[0].test.value = xmp[0].innerHTML.replace(/<br>/g,'\\n');
} onload = xmp;
</script></head>

<body>
<xmp>
<div id="menu_1" class="menu" style="left:0px">
<br>
<a href="target1.html"> Item 1 </a>
<br>
<a href="target2.html"> Item 2 </a>
<br>
<a href="target3.html"> Item 3 </a>
<br>
</div>
</xmp>

<form>
<textarea cols="84" rows="12" name="test"></textarea>
</form>

JohnKrutsch
07-09-2004, 06:45 AM
For all kinds of reasons that I wish did not exist I have to take plain text from a form and format it with very basic html before it gets sent to another form. Some the text however contains coding examples found with the <xmp> tags. Turning all line breaks into <br /> tags works for every situation I ran into except where the code examples show up. So I ended up needing to replace all the line breaks with <br /> tags except for the cases where the line breaks were contained within the <xmp> tags.

I know it is confusing, my head still hurts. But this is how I ended up accomplishing the task with neofibril's help:



msg=msg.replace(/\n/g,'<br />');
rgx=/<xmp>([\w|\t|\r|\W]*?)<\/xmp>/g;
arr=msg.match(rgx);
msg=msg.replace(rgx, function(){return arr.shift().replace(/<br \/>+/g, "\n")});



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum