How to properly escape characters in regexp

后端 未结 4 1828
失恋的感觉
失恋的感觉 2020-11-30 08:59

I want to do a string search inside a string. Simply saying MySTR.search(Needle).

The problem occurs when this needle string contains speci

4条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 09:45

    I'm just dipping my feet in Javascript, but is there a reason you need to use the regex engine at all? How about

    var sNeedle = '*Stars!*';
    var sMySTR = 'The contents of this string have no importance';
    if ( sMySTR.indexOf(sNeedle) > -1 ) {
       //found it
    }
    

提交回复
热议问题