doujiepin5547
2013-12-01 02:57
浏览 23

如何检查字符串的一部分并接受其后的任何内容?

I had a hard time wording this question. Lets say you have two things in a database:

-"String"

-"Stringify"

In PHP, you use this: "SELECT * WHERE string = stri%"

This would return both of the strings in the database because the % [percentage sign] is the responsible one for checking if the values have those four letters [stri] and accepting them since the rest doesn't matter.


That's not the question. Is there something like that [%] in jQuery?

I have the following code.

if (ext == "jpg" || ext == "gif") {
} else {
   ext = ext + ".jpg";
}

Many times, my JSON might return something like this:

nameofimage.jpg?1

This is ignored because jpg?1 is NOT equals to jpg but it is still a jpg. This is why I am asking... Is there anything that can be used to check only the first three letters of the extension and ignore the rest?

Something like this:

if (ext == "jpg%" || ext == "gif%") {
//Do nothing, it's already a jpg or a gif
} else {
   ext = ext + ".jpg";
}

I tried my very best to word this the right way. I hope someone here can help me. Thank you.

图片转代码服务由CSDN问答提供 功能建议

我很难写出这个问题。 假设您在数据库中有两件事:

- “String”

- “Stringify”

在PHP中,您使用: “SELECT * WHERE string = stri%”

这将返回数据库中的两个字符串,因为 [百分号]负责检查这些值是否具有这四个字母[stri]并接受它们,因为其余的无关紧要。


这是 不是问题。 在jQuery中有类似 [%] 的东西吗?

我有以下代码。

  if  (ext ==“jpg”|| ext ==“gif”){
} else {
 \ text = \ text +“.jpg”; 
} 
   
 
  

很多时候,我的JSON可能会返回如下内容:

nameofimage。 jpg?1

这是 被忽略,因为 jpg?1 NOT 等于 jpg ,但它仍然是jpg。 这就是我要问的原因......有没有什么可以用来检查扩展名的前三个字母而忽略其余的字母?

这样的事情:

  if(ext ==“jpg%”|| ext ==“gif%”){
 //什么都不做,它已经是jpg或者gif 
}其他{\  n ext = ext +“.jpg”; 
} 
   
 
 

我尽力以正确的方式说出来。 我希望这里有人可以帮助我。 谢谢。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dortmundbvb0624 2013-12-01 03:00
    已采纳

    Try string.contains()

    if(ext.contains(".jpg") || ext.contains(".gif")) {
    
    已采纳该答案
    打赏 评论
  • duan35557593 2013-12-01 03:05

    I think the method you are looking for is indexOf():

    if (ext.indexOf('jpg') !== -1 || ext.indexOf('gif') !== -1) {
        // true
    }
    

    However it is a bit cleaner and safer to not just check for the extension but the whole filename as well. Imagine this filename: "somejpg.exe". The above method would consider it as a "jpg" where as it is not. The easiest to use regular expressions for these kind of things:

    /\.(jpg|gif|png|bmp)$/.test('test.png'); // => true
    /\.(jpg|gif|png|bmp)$/.test('test.gif'); // => true
    /\.(jpg|gif|png|bmp)$/.test('hack_jpg.exe'); // => false
    
    打赏 评论

相关推荐 更多相似问题