Javascript子字符串检查使用indexOf或搜索日期字符串与正斜杠/ [英] Javascript substring check using indexOf or search on a date string with forward slash /

查看:320
本文介绍了Javascript子字符串检查使用indexOf或搜索日期字符串与正斜杠/的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很惊讶,没有找到任何关于这个的帖子,我一定很想念一些非常微不足道的事情。我有一个小的JavaScript函数来检查一个字符串是否匹配对象的属性。简单的东西吧?除了包含正斜杠之外的所有字符串,它可以很容易地工作。

 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆