monkeytooth Posted July 23, 2011 Share Posted July 23, 2011 Lets say I have a string var myString = "I was sitting at the green light thinking about the Green Hornet and thought about the greenish tints he had on the windows"; I need to split the string so its broken down by spaces like a CSV but just the space.. Then I need to find every instance of "green" but case sensitive, any ideas? I thought I had an idea but I just ended up doing something entirely wrong... What I need my output to look like is 20, 45, 50.. (thats not exact but just for the sake of sample). What i need to know is the indexOf() splitting it into an array is for something else. which the spliting I have an understanding of.. Its the indexOf for multiple instances thats got me confused. Quote Link to comment https://forums.phpfreaks.com/topic/242677-split-a-string-and-then-find-the-indexof-multiple-instances-of-a-string/ Share on other sites More sharing options...
JasonLewis Posted July 25, 2011 Share Posted July 25, 2011 You want to split it by the space first, so that it's an array? I'm a bit confused by your question. With the indexOf() method you can supply a second parameter as a start point, however the issue will come up when indexOf() actually finds nested words. So in your example it would find the "green" at the start of "greenish". The other way you can do it is by using regexp in JS, with match(). Of course match will only find the occurring strings and not actually return the location of them. What exactly do you need this for, might be easier to provide a better and more suited solution. Quote Link to comment https://forums.phpfreaks.com/topic/242677-split-a-string-and-then-find-the-indexof-multiple-instances-of-a-string/#findComment-1246579 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.