Determining whether a string has a substring (word)

后端 未结 5 1186
你的背包
你的背包 2020-12-12 07:32

I try to use a conditional to verify if a string contain a certain word, for example: I want to use a method (regex?) to find if a string has the text \"&SWE>cl

相关标签:
5条回答
  • 2020-12-12 08:12
    if (/&SWE>clickable/g.test(text2)) {
        // exists
    }
    

    EDIT: Using indexOf like others have posted might be better, since it’s more readable and you don‘t need to escape characters. And arguably faster :/

    0 讨论(0)
  • 2020-12-12 08:16

    try this :

    if (text1.indexOf('&SWE>clickable')>=0){ ... }
    

    or regex way :

    var re = new RegExp('\&SWE\>clickable')
    if (re.test(text1)){ ... }
    
    0 讨论(0)
  • 2020-12-12 08:17
     if (text2.indexOf("&SWE>clickable") > -1) {
        ....
    
    0 讨论(0)
  • 2020-12-12 08:29
     if(text1.indexOf(text2)) 
        document.write ("the layer is clickable") 
     else 
        document.write ("the layer is not clickable")
    
    0 讨论(0)
  • 2020-12-12 08:31

    You can use String.indexOf. It returns -1 if the string is not found, otherwise it returns the index where the string was found. You can use it like this:

    if (s.indexOf("&SWE>clickable") !== -1) { ... }
    
    0 讨论(0)
提交回复
热议问题