SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist
    Join Date
    Dec 2005
    Posts
    527
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    position of a word

    How can I find the positin of a word in with javascript

  2. #2
    malloc
    Join Date
    Nov 2006
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you clarify exactly what you mean?

    Ie for "the quick brown fox jumped over the lazy dog"
    for "fox" do you want the word-index (3), the character index (16), or something else?

    Either way, I'd recommend using regular expressions to do the word matching.

  3. #3
    SitePoint Evangelist
    Join Date
    Dec 2005
    Posts
    527
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is my string : "the quick brown fox jumped over the lazy dog"
    I need the place of "fox"
    The result is 16
    PHP have a function for it :
    strpos(string,word)
    I need something like this in javascript

  4. #4
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like so.

    mytest = "this is a test"
    pos = mytest.indexOf('test')
    alert(pos)

    Hope this helps.

  5. #5
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    FYI there's also a str.lastIndexOf() "method" as well.
    Best,
    max

  6. #6
    malloc
    Join Date
    Nov 2006
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    None of the above will find the position of a word, they all find substrings.
    Ie:
    "He was doing the foxtrot, that fox"
    will return 17, not 31.

  7. #7
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My mistake. I thought that was what he was looking for as php's strpos also returns substring positions given strpos(str,word).


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •