Javascript子字符串检查使用indexOf或搜索日期字符串与正斜杠/ [英] Javascript substring check using indexOf or search on a date string with forward slash /
问题描述
04/08/2015 .indexOf('4')// return 2:good
04/08/2015.indexOf('4 /')//返回-1:为什么?
同样的问题似乎也是.search()函数。我在日期字符串中遇到这个问题。
请注意,出于性能原因,我不想使用基于正则表达式的解决方案。感谢您的帮助提前!
您的字符串中有不可见的Unicode字符。
如果您输入的是从左到右的标记(十六进制200E)出现在两个斜线字符周围,以及字符串的开头和结尾。您的浏览器控制台上的代码,而不是剪切和粘贴,您会看到它按预期工作。
I am surprised to not to find any post regarding this, I must be missing something very trivial. I have a small JavaScript function to check if a string matches an object's properties. Simple stuff right? It works easily with all strings except those which contain a forward slash.
"04/08/2015".indexOf('4') // returns 2 :good
"04/08/2015".indexOf('4/') // returns -1 :why?
The same issue appears to be with .search() function as well. I encountered this issue while working on date strings.
Please note that I don't want to use regex based solution for performance reasons. Thanks for your help in advance!
Your string has invisible Unicode characters in it. The "left-to-right mark" (hex 200E) appears around the two slash characters as well as at the beginning and the end of the string.
If you type the code in on your browser console instead of cutting and pasting, you'll see that it works as expected.
这篇关于Javascript子字符串检查使用indexOf或搜索日期字符串与正斜杠/的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!