我使用这段代码,我的问题是是否有比 indexOf 更好的方法来检查字符串:
if(documentFile.ending.indexOf('pdf') > -1 || documentFile.ending.indexOf('PDF') > -1 || documentFile.ending.indexOf('docx') > -1)
最佳答案
ES6有 bool 函数。使用:
if ( documentFile.ending.includes('pdf') ) { }
或者对于正则表达式:
if ( documentFile.ending.match(/your-regex/) { }
示例规范:https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Global_Objects/String/includes
关于Javascript - 有没有更好的方法来检查字符串而不是indexOf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41615650/