Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <1805042.BzeuYpbCYS@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Fri, 02 Nov 2012 18:41:48 +0100 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: 63 NNTP-Posting-Date: 02 Nov 2012 18:41:48 CET NNTP-Posting-Host: b584de3b.newsspool2.arcor-online.net X-Trace: DXC=9BfCmB=?cC9NTD55K=A9EHlD;3Yc24Fo<]lROoR18kFK0Nj95_PeH=`6DnBdnP;Acm1 X-Complaints-To: usenet-abuse@arcor.de Xref: csiph.com comp.lang.javascript:17020 Patricia Shanahan wrote: > On 11/2/2012 2:22 AM, Evertjan. wrote: > ... >> if ( /foo/.test(someString) ) {..} >> >> I trust not understanding Regex is not a valid counterargument. > > If I'm prepared to look at it long enough, I can generally work out what > a Regex does, but Regex does not seem to me to be a very human-friendly, > smoothly readable language. That depends on the flavor and your experience with them. > For this particular case, it looks OK if the probe really is a literal > such as "foo". In exactly that case using a Regular Expression is overkill. You need to consider that for every RegExp literal in the code (and per ES 5.x in every use of that literal), a new RegExp instance is being created. > Could you show me the code you would use for this if the probe were an > actual parameter or variable with unknown contents? > > That is, what would you use to replace: > > if(someString.indexOf(someOtherString)) != -1) It is possible with if ((new RegExp(someOtherString)).test(someString)) but I would not use that. However, the following can be useful and necessary: if ((new RegExp(someOtherString, "i")).test(someString)) The caveat in both cases is that any special characters in the value of `someOtherString' that should not assume their special meaning need to be escaped. This can be accomplished with a previous replace() – if ((new RegExp(someOtherString.replace(/…/g), "i")).test(someString)) – or with a user-defined method: if ((new RegExp(jsx.regexp.escape(someOtherString), "i")) .test(someString)) One could also augment the ECMAScript String prototype object: if ((new RegExp(someOtherString.regexpEscape(), "i")) .test(someString)) See jsx.dom.css.addClassName() for a working example of a variable RegExp: PointedEars -- Danny Goodman's books are out of date and teach practices that are positively harmful for cross-browser scripting. -- Richard Cornford, cljs, (2004)