开发者

JQuery .text() cannot use indexOf to find spaces

开发者 https://www.devze.com 2023-03-15 10:09 出处:网络
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

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 th开发者_运维问答is problem before? Does anyone have any other ideas?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号