Forum Moderators: open

Message Too Old, No Replies

Searching for Tags

         

DanBWeb

12:23 pm on Sep 21, 2004 (gmt 0)

10+ Year Member



Hello,

Here is what I am trying to do.....

I have a textarea/editor. When people cut-n-paste from Word, I want to search for <Span> tags and alert them that the content has formatting.

I can't seem to make Javascript find either <Span or &lt;Span

Any ideas?

Dan B

Rambo Tribble

1:15 pm on Sep 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<body>
<script type="text/javascript">
var str_x="Stuff <span> and more stuff";
alert(str_x.indexOf("<span"));
</script>
</body>
</html>

In other words, if indexOf()!=-1, then the string exists.

Bernard Marx

2:31 pm on Sep 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think that deserves a regular expression / test.
MS apps are likely to produce upper-case tags (certainly wouldn't put it past them).
Your call.

Rambo Tribble

3:25 pm on Sep 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Alternatively, since the condition is likely to be tested in an if statement, the and operator could be employed:

if(string.indexOf("<span")!=-1 && string.indexOf("<Span")!=-1 && string.indexOf("<SPAN")!=-1);

Bernard Marx

4:01 pm on Sep 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Look here, Tribble. That's all very well, but not quite good enough in our never-ending pursuit of excellence.

The alternatives are:
#1

[blue]if( string.toLowerCase().indexOf("<span")!= -1 );[/blue]

or
#2

[blue]if( /<span/i.test(string) )[/blue]

#1 is actually faster (half of nothing)
#2 is neater, and can, of course, be developed to test for other tags.

... and from the gentletribble who has trouble typing n'all!

Rambo Tribble

4:30 pm on Sep 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Consider this tribble properly put in his place. (though I have been given the impression that regex's are, in fact, quite overhead intensive)

Bernard Marx

5:47 pm on Sep 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think it's the creation of the RE object that's the key. When I had nowt better to do, I did a speed trial, #1 vs #2

I found that #1 was twice as fast as #2, in that particular scenario. However, if there's some looping going on, and the RE is used multiple times, then it can be created outside the loop. The time difference then disappears.

Having said that, the time in either case is meaninglessly small, so it's a case of taste - which one you think looks nicer. Maybe, though, there are browsers out there with dodgy RE support.

DanBWeb

6:01 pm on Sep 21, 2004 (gmt 0)

10+ Year Member



so many choices too try....

Thanks!

Dan B