Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!nuzba.szn.dk!pnx.dk!fu-berlin.de!uni-berlin.de!not-for-mail From: Jake Jarvis Newsgroups: comp.lang.javascript Subject: Re: regexp help Date: Fri, 07 Sep 2012 10:55:17 +0200 Lines: 27 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de KZZ0HW78W5aub+GRbsR2IwBwKffmPra9b5sSrtY5M1Hk+FrcI4CxHsKaa6YorRWQ8= User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:15.0) Gecko/20120824 Thunderbird/15.0 In-Reply-To: Xref: csiph.com comp.lang.javascript:15760 On 07.09.2012 06:37, wrote Andrew Poulos: > I have a string > var str = 'This is some text.'; > > Within that string I need to find whether a whole word exists. The test > is to be case insensitive. The word to test for is held in the var 'k'. > > When I try this: > > var k = 'SOME'; > var myRe = new RegExp("b" + k + "b", "i"); > var found = myRe.test(str); > > I expected 'found' to return true. What am I doing wrong? > You are not escaping the two 'b' characters. There are 2 levels here: the level of the regular expression in which you want '\b', and the level of the pattern string in which you need '\\b', because strings have their own escape rules which apply first and under those '\b' means "backspace character", whereas '\\b' means "backslash b". -- Jake Jarvis