473,387 Members | 1,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

replace *except* where...

Hi All,

I have the following to replace newline chars with <br> in a string:

..replace(/\n/g,"<br>")

How can I change this so that it replaces only if there is not already a
"<br>newline" or "newline<p>" combo?

Thanks for any advice!

Rob
:)
Jul 23 '05 #1
4 3806
Robert Mark Bram wrote on 27 sep 2004 in comp.lang.javascript:
I have the following to replace newline chars with <br> in a string:

.replace(/\n/g,"<br>")

How can I change this so that it replaces only if there is not already a
"<br>newline" or "newline<p>" combo?


if(/#$#$#$#/.test(t))alert("error")

t=t.replace(/\n/g,"#$#$#$#")
..replace(/#$#$#$#<p>/g,"\n<p>")
..replace(/<br>#$#$#$#/g,"<br>\n")
..replace(/#$#$#$#/g,"<br>")

// all replaces on one line please !!

not tested

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Jul 23 '05 #2
Hello Evertjan!
.replace(/\n/g,"<br>")
How can I change this so that it replaces only if there is not already a
"<br>newline" or "newline<p>" combo?


if(/#$#$#$#/.test(t))alert("error")

t=t.replace(/\n/g,"#$#$#$#")
.replace(/#$#$#$#<p>/g,"\n<p>")
.replace(/<br>#$#$#$#/g,"<br>\n")
.replace(/#$#$#$#/g,"<br>")

// all replaces on one line please !!


#$#$#$# seemed to cause a few problems. Instead of #$#$#$# I used jxjxjxjx
and it works well - thank you very much!

Rob
:)
Jul 23 '05 #3
Robert Mark Bram wrote:
I have the following to replace newline chars with <br> in a string:

.replace(/\n/g,"<br>")

How can I change this so that it replaces only if there is not already a
"<br>newline" or "newline<p>" combo?


So long as you don't mind losing the unwanted newlines, the simplest thing
would be to remove them before replacing the ones you do want e.g.:

s.replace(/\n*(<(br|p)>)\n*/g, '$1').replace(/\n/g,'<br>')

(split the regex up if you don't want to catch newline<br> and <p>newline
also.)
Jul 23 '05 #4
Robert Mark Bram wrote:
Hi All,

I have the following to replace newline chars with <br> in a string:

.replace(/\n/g,"<br>")

How can I change this so that it replaces only if there is not already a
"<br>newline" or "newline<p>" combo?


Although the end tag for a paragraph is listed as optional <url:
http://www.w3.org/TR/REC-html40/struct/text.html#edef-P />, you should get
used to coding a paragraph as:

<p>Paragraph text</p>

rather than:

Paragraph text<p>

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sorin Marti | last post by:
Hello all, I am quite new to python and want to do the following: - open a file (done) - read each line (done) - search a pattern in the file and replace it - write the new file I want to...
4
by: beliavsky | last post by:
The code for text in open("file.txt","r"): print text.replace("foo","bar") replaces 'foo' with 'bar' in a file, but how do I avoid changing text inside single or double quotes? For making...
4
by: Prasad S | last post by:
Hello I wish to replace all the characters in a string except those which are inside '<' & '>' characters. And there could be multiple occurences of < & > within the string. e.g. string =...
19
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not...
2
by: bj daniels | last post by:
I am not certain which group to post this to - sorry if this is the wrong group: I have a table that holds the location of various media and such (mostly images). One field is the Media name,...
2
by: jason | last post by:
Hello. I just converted some data to to sql2000 into a TEXT type field. I needed to remove all tabs from the data and now want to remove extra lines too. As we know, SQL2000's replace function...
4
by: serge | last post by:
I managed to put together C# code and have it do the following: 1- Get all the table names that start with the letter "Z" from sysobjects of my SQL 2000 database and put these table names...
3
by: gregpinero | last post by:
Hi guys, What I'm trying to do is find all instances of an acronymn such as IBM on a webpage and replace it with <acronym title="International Business Machines">IBM</acronym>. However in my...
17
by: Levidikus | last post by:
Normally, I never have any problems with String.Replace(). However, I found that I need to replace multiple instances of the character "ª" (\xAA) with a # symbol. The input file is a simple one...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.