|
Post by Malagrond on Feb 23, 2011 13:35:12 GMT -6
In JavaScript, one of the most commonly used methods of checking a string is the match() method. It's relatively straight-forward to use and can be use on virtually every textual piece of data.
For example, if you wanted to check an input string for a specific value:
var inputString = "Bob went to the store, but I think he's coming back any minute now."; var lookFor = /store/; (inputString.match(lookFor)) ? alert("Found it!") : alert("Not there!");
match() returns a boolean value (true/false), so it can be used in an if statement. It may sometimes fail, so it is best to include the g and i flags, like so:
var lookFor = /store/gi;
The g stands for global, meaning it will search the entire string for multiple occurrences. You can use this to your benefit for other tasks, but that's for another topic on using Regular Expressions. The i stands for case insensitive, meaning it will match the string regardless of letter case and surrounding characters.
|
|