JQuery.text () cannot use indexOf to find spaces

It is odd.

I have a list item containing the text 'May 13, 2011'. I have many such dates, and I want to use jQuery to search for them using free text input (they do not always refer to dates), but I can not find anything if I put a space in the search field.

but

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 inserted my return text into a converter with the text in hexadecimal, and the whitespace is “20” (32 in decimal, this is a space in ASCII), so this is not a funny space character.

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

+7
source share
1 answer

Answering my question. Thanks to everyone who helped me along the way by leaving comments!

All tests with this list item worked the way they should, except for the real version on my machine! For some reason, this is not a space, it is an ASCII character 160 (non-breaking space, HTML object   )

Further research shows

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

I will not doubt why, at least, now it works: D

+7
source

All Articles