Dave's Brain

Browse - programming tips - javascript search a string for another string

Date: 2009jul1
Language: javaScript

Q.  How can I search for a string in another string?

A.  You can use match() but if you don't need/want to look for a pattern,
use indexOf() like this:

	function isThere(hay_stack, needle) {
		return hay_stack.indexOf(needle) >= 0;
	}

	// Example use:
	function isFirefox() {
		return isThere(navigator.userAgent, 'Firefox');
	}
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.
Advertisements: