7

This is an odd one.

I have a list item, containing the text '13 May 2011'. I have a lot of these dates, and I want to use JQuery to search them by a free text input (they're not always dates), but I can't seem to search for anything if I put a space in the search box.

However,

li.text() // 13 May 2011
li.text().indexOf('13') // 0
li.text().indexOf('13 ') // -1
li.text().indexOf(' ') // -1
'13 May 2011'.indexOf('13') // 0
'13 May 2011'.indexOf('13 ') // 0
li.text() == '13 May 2011' // false

I've pasted my return text into a text-to-hex converter, and the space character is a '20' (32 in decimal, which is a space in ASCII), so it's not a funny space character.

Has anyone encountered this problem before? Does anyone have any other ideas?

12
  • Interestingly, the results I get in Chrome are: "13 May 2011", 0, 0, 2, 0, 0, true. You can see it here. Commented Jun 23, 2011 at 14:49
  • 1
    @Connell what browser are you using to test with? Commented Jun 23, 2011 at 14:50
  • 1
    I'm using Chrome 12's console to get them results. I'm just about to test in Firefox 4 Commented Jun 23, 2011 at 14:51
  • I'm using Chrome 12, and it works for me: jsbin.com/asaki4/2/edit Commented Jun 23, 2011 at 14:52
  • Here is what i get: jsfiddle.net/maniator/HTdzB (Chrome 12) results: 13 May 2011 0 0 2 0 0 true Commented Jun 23, 2011 at 14:52

1 Answer 1

7

Answering my own question. Thanks to all those that helped me along the way by leaving comments!

All tests with this list item have worked as they should except the real version on my machine! For some reason, it's not a space, it's ASCII character 160 (a non-breaking space, HTML entity  )

Further investigation shows

hex(li.text()) // 31 33 a0 4d 61 79 a0 32 30 31 31
li.text().indexOf('13'+String.fromCharCode(160)) // 0

I'm not going to question why, at least it works now :D

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.