怎么检查字符串是否含有以下格式

CaixK
  • 370

检测字符串是否含有以下格式['wmv','asf','asx','mp4','3gp']

回复
阅读 735
3 个回答
const match = (str) => {
  return ['wmv','asf','asx','mp4','3gp'].some(v => 
    str.indexOf(v) !== -1)
}
let res = ['wmv','asf','asx','mp4','3gp'].includes('wmv')
match = (str) => {
  return ['wmv','asf','asx','mp4','3gp'].some(v => 
    //     str.indexOf(v) !== -1)
    (new RegExp(v+'$')).test(str)
  )
}
match = (str) => {
  return ['wmv','asf','asx','mp4','3gp'].some(v => 
        str.slice(3).indexOf(v) !== -1
//     (new RegExp(v+'$')).test(str)
  )
}

用正则匹配,或者截取后几位。当然,你还可以把.也加上

image.png

image.png

宣传栏