Dave's Brain

Browse - Programming Tips - How can I search for a string in another string?

Date: 2009jul1
Language: javaScript
Level: beginner

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'); }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, 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.