Tag Replace

N

nithicorent

Hi,

How to replace a tag by space.
Example:


var str="<b>abcd</b>";

i need a str with "abcd";
replace <b> and </b> by ""
 
G

GArlington

Hi,

How to replace a tag by space.
Example:

var str="<b>abcd</b>";

i need a str with "abcd";
replace <b> and </b> by ""

See RegExp (regular expressions) tutorial on the net
 
A

Amrit Ranjan

See RegExp (regular expressions) tutorial on the net

This will replace all html tags from the input string i.e str
alert(str.replace(/<([a-z]*)>(.*)<\/\1>/i, "$2"));

1 2 3
1. Tag match
2. The string which need to retain.
3. End tag match (back references)

Thanks
 
G

Georgi Naumov

news:b28583f3-be65-4c45-b1c4-65a06d931091@i72g2000hsd.googlegroups.com...
On Feb 4, 1:03 pm, (e-mail address removed) wrote:
Hi,
How to replace a tag by space.
Example:
var str="<b>abcd</b>";
i need a str with "abcd";
replace <b> and </b> by ""
See RegExp (regular expressions) tutorial on the net
This will replace all html tags from the input string i.e str
alert(str.replace(/<([a-z]*)>(.*)<\/\1>/i, "$2"));
1 2 3
1. Tag match
2. The string which need to retain.
3. End tag match (back references)
Thanks
"all html tags"?

This should have said ANY (single) opening and closing html tag
pair...


That converted:
<html><body><b>Hello World!</b></body></html>
to
<body><b>Hello World!</b></body>
Shoud it convert it to just:
Hello World

Other version. Just extending String object.
String.prototype.stripTags = function(aRep)
{
return this.replace(/<[^>]+(?:\/[^>]*)?>/g,aRep);
}
var mystring = "<b>test</b> <i>second test</i>";
alert(mystring.stripTags(" "));
 
T

Thomas 'PointedEars' Lahn

Amrit said:
See RegExp (regular expressions) tutorial on the net

This will replace all html tags from the input string i.e str
alert(str.replace(/<([a-z]*)>(.*)<\/\1>/i, "$2"));

No, it won't. Try it on <a href="foo"> for a start. The correct expression
to match *any* SGML-conforming tag (HTML tag would be a *restriction* that
would not be easy to fulfill with a Regular Expression), being posted here
numerous times, is

/<[^>]*>/


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,373
Latest member
Desiree036

Latest Threads

Top