Match dynamic string using regex

后端 未结 4 541
旧巷少年郎
旧巷少年郎 2020-12-02 00:26

I\'m trying to detect an occurrence of a string within string. But the code below always returns \"null\". Obviously something went wrong, but since I\'m a newbie, I can\'t

4条回答
  •  心在旅途
    2020-12-02 00:44

    The right tool for this job is not regex, but String.indexOf:

    var str = '32:width: 900px;',
        search = 'width',
        isInString = !(str.indexOf(search) == -1); 
    
    // isInString will be a boolean. true in this case
    

    Documentation: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/String/indexOf

提交回复
热议问题