Testing for characters in JS string

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
mzfp2
Forum Contributor
Posts: 137
Joined: Mon Nov 11, 2002 9:44 am
Location: UK
Contact:

Testing for characters in JS string

Post by mzfp2 »

simple question using JavaScript, how can i determine if a string contains certain characters, eg qoutes or asterisks?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

use regEx - yes, i know it's a fantastic pain in the b**t that you can even do them in JS, but hey: it's been certified as a good thing, even Bill Gates and Richard Stallman do the regEx-jiggle together.

Coming back to your question: use Javascript's search (don't have a link to a tutorial for that, but have a look at the following example)

Code: Select all

<html><head><title>Test</title>
</head><body>
<script type="text/javascript">
<!--
 var Aussage = "Hello there, sky is blue, sun is bright, life is just like dynamite.";
 var Ergebnis = Aussage.search(/dynamite.+/);
 if(Ergebnis != -1)
  alert("found");
//-->
</script>
</body></html>
Post Reply