/    Sign up×
Community /Pin to ProfileBookmark

var.replace is not a function.. What?

I have been strugglign with this function.

It says that str.replace is not a function, any ideas why?

The function is suppose to strip tags (<something></something>) from the input.

[CODE]function st(ip)
{
var str = ip;
return str.replace(/<(.*)>/g,””);
}[/CODE]

Thanks!
Chris

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@NedalsApr 06.2008 — It says that str.replace is not a function, any ideas why?[/QUOTE]
I'm not getting that error.

BUT, and I'm guessing, you will not get what you expect.

I think this is what you want.
<i>
</i>var text = "&lt;tag&gt;word&lt;/tag&gt;";
var data = st(text);
alert(data)

function st(ip) {
var str = ip;
return str.replace(/&lt;.*?&gt;/g,"");
}
Copy linkTweet thisAlerts:
@aximbigfanauthorApr 06.2008 — I'm not getting that error.

BUT, and I'm guessing, you will not get what you expect.

I think this is what you want.
<i>
</i>var text = "&lt;tag&gt;word&lt;/tag&gt;";
var data = st(text);
alert(data)

function st(ip) {
var str = ip;
return str.replace(/&lt;.*?&gt;/g,"");
}
[/QUOTE]


That words!! Thanks!

Chris
Copy linkTweet thisAlerts:
@aximbigfanauthorApr 06.2008 — Any ideas how I could extract the value of what is between the tags?

Thanks!

Chris
Copy linkTweet thisAlerts:
@NedalsApr 06.2008 — Here's one way...
<i>
</i>function st(ip) {
var str = ip;
return str.replace(/&lt;.*?&gt;/g,"&lt;&gt;");
}

[b]Update[/b]

Ops! Not what you asked for. I'll be back

If you have just one tag in ip, it's easy.
<i>
</i>return str.replace(/&lt;(.*)?&gt;/g,"$1");

If you have multiple tags it's a lot more tricky.
Copy linkTweet thisAlerts:
@NedalsApr 06.2008 — That last post of mine does not work. ?

Parsing HTML is not the easiest thing to do.

This will extract the tag name of only the first tag in a line.
<i>
</i>var text = "some text followed by a &lt;tag&gt;word&lt;/tag&gt;and more text";
var data = st(text);
alert(data)

function st(ip) {
var str = ip;
return str.replace(/^.*?&lt;(.*?)&gt;.+$/,"$1");
}

If you have multiple or nested tags, it can get really complicated.
Copy linkTweet thisAlerts:
@aximbigfanauthorApr 06.2008 — That last post of mine does not work. ?

Parsing HTML is not the easiest thing to do.

This will extract the tag name of only the first tag in a line.
<i>
</i>var text = "some text followed by a &lt;tag&gt;word&lt;/tag&gt;and more text";
var data = st(text);
alert(data)

function st(ip) {
var str = ip;
return str.replace(/^.*?&lt;(.*?)&gt;.+$/,"$1");
}

If you have multiple or nested tags, it can get really complicated.[/QUOTE]



Nothing nested, no, basically what it is is that I am going for a sort of XMLish format. Anyway, I got it working, thanks!!

Chris
×

Success!

Help @aximbigfan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...