CoverYourASP --> Snippets

Free membership

Join in the fun! Sign in
Member Services

Site navigation
Download the entire site!
Search my articles
Free Magazines
Browse the directory

Send me feedback
Buy my boxer shorts

Recommend this page
Printer-friendly page

Resources I recommend
Link to my site
Advertising slashed!
About your privacy
Legal stuff
Site statistics
25 active users
2147 visitors today
1907 pages today
(only part of today)
Tools I use

CoverYourASP
Copyright © 1999-2016 James Shaw.
All rights reserved.

ASP.NET Blog
RSS submissions
E-commerce

Now open source with SourceForge!

"Snippets" are mini-articles - they allow me to very quickly add answers to your frequently asked questions, and add brief explanations for topics that you've searched for without success. So, everytime you send an email or perform a search, chances are a new snippet will be the result!

18 Sep: Using regular expressions to remove linefeeds and HTML

Sing Cheng:

"I met problems again when doing a guestbook. In the TEXTAREA of my guestbook form, when i enter

First line
Second line
Third line

then I submit my form and post the record into my database. But when it display, it will display as

First line Second line Third line

So, I am asking your help! Could you please tell me how to insert the line break code (<BR>) into each line of the text in the TEXTAREA before it is submitted into the datebase?

Also, please do tell me how to remove HTML tags from my text before being submitted into the database.

Thank you very much and I will very appreciate your help."

Me:

Both problems are easily fixed using regular expressions. To replace linefeeds with <br>, use this statement:

sStr = sStr.replace ( /\n/g, '<br>' );

To remove all HTML from a string, try this:

sStr = sStr.replace ( /<[^>]*>/g, '' );

Featured sponsor
My favorite resources


New Proposal Kit Professional 5.1
Brand yourself as a top professional: create quotes and amazing proposals and get many legal documents free!

The latter saved me 3 times the purchase price on the first day I owned it!


See my source code
wherever you see this icon...

You can also download the entire site source code for FREE!