Forum Moderators: open
<!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.
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!
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.