Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsfeed.straub-nv.de!uucp.gnuu.de!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <1492793.DGNhQbDWfW@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Fri, 07 Sep 2012 15:36:23 +0200 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 8Bit X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 47 NNTP-Posting-Date: 07 Sep 2012 15:36:25 CEST NNTP-Posting-Host: 223ad3e2.newsspool1.arcor-online.net X-Trace: DXC=e\E 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". In addition, \b is not sufficient in most cases as it matches a boundary between a "word character" and "not-a-word-character". Word characters are those matched by `\w', which in turn is defined as the equivalent of [0-9A- Za-z_]. Non-ASCII letters are not included. Even basically English text can contain non-ASCII characters (borrowed from or written in other languages): f. Which gives me the idea that a Unicode mode for jsx.regexp.RegExp() to replace \b, \w and \W – in addition to or using the already implemented Unicode character properties (\p{Lu} et al.) – would come in handy. Comments anyone? PointedEars -- Sometimes, what you learn is wrong. If those wrong ideas are close to the root of the knowledge tree you build on a particular subject, pruning the bad branches can sometimes cause the whole tree to collapse. -- Mike Duffy in cljs,