Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #31569
| Path | csiph.com!news.mixmin.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Newsgroups | comp.lang.javascript |
| Subject | Re: Filter array of objects for property |
| Date | Thu, 13 Oct 2016 11:21:15 +0100 |
| Organization | PointedEars Software (PES) |
| Lines | 88 |
| Message-ID | <1770295.oMNUckLgyt@PointedEars.de> (permalink) |
| References | <IKWdnYrudsxAIWPKnZ2dnUU7-XXNnZ2d@westnet.com.au> <877f9d87y0.fsf@bsb.me.uk> |
| Reply-To | Thomas 'PointedEars' Lahn <cljs@PointedEars.de> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="UTF-8" |
| Content-Transfer-Encoding | 8Bit |
| X-Trace | solani.org 1476354076 1223 eJwFwQkBwDAIA0BL0CY8cigD/xJ2x2tq7TAauFz5iKOCOmDK2dhiPHSMTWqnelblbeLp+PoPBucQqg== (13 Oct 2016 10:21:16 GMT) |
| X-Complaints-To | abuse@news.solani.org |
| NNTP-Posting-Date | Thu, 13 Oct 2016 10:21:16 +0000 (UTC) |
| User-Agent | KNode/4.14.2 |
| Cancel-Lock | sha1:wY62U2uBycmk6Dh0BbTNnyMALc8= |
| X-NNTP-Posting-Host | eJwNyckBwCAMA7CVYogd1qE59h+B6ituQRkuyjmcECbnogn0XWrIyvcfpUn2WVKZKs5nVrQHIMkQ/A== |
| X-User-ID | eJwFwYEBACAEBMCVCs83jtD+I3QH9e0V5nDDw9ND7ZVokiEmsvw52mTuSnXetC1ECurK5oxGVM9grHhGPy3OFQw= |
| Xref | csiph.com comp.lang.javascript:31569 |
Show key headers only | View raw
Ben Bacarisse wrote:
> Andrew Poulos <ap_prog@hotmail.com> writes:
>> If I have an array that looks like this
>>
>> var arr =
>> [
>> {
>> a: "foo_2",
>> b: "trigger_1",
>> c: "bar_3"
>> },
>> {
>> a: "foo_3",
>> b: "foo_4",
>> c: "bar_4"
>> },
>> {
>> a: "foo_6",
>> b: "trigger_5",
>> c: "bar_5"
>> }
>> ...
>> ];
>>
>> and to get an array of all "b" values that start with "trigger_" and
>> end with a number I'm calling
>>
>> function getValues(theArr, pre) {
>> var i = 0,
>> fnd = [],
>> tmp;
>>
>> for (i = 0; i < theArr.length; i++) {
>> tmp = theArr[i].b;
>> if ( /trigger_\d+$/.test(tmp) ) {
>> fnd.push(tmp);
>> }
>> }
>>
>> return fnd;
>> }
>>
>> While this works I'd like the argument "pre" to be a string I pass to
>> the regexp so that I can test other values and not just "trigger_" but
>> I don't know how.
>
> You can construct a RegExp from a string:
>
> RegExp(pre + '\\d+$').test(tmp)
First of all, the original regular expression does _not_ match strings that
*start* with “trigger_”; it matches all those that *contain* “trigger_”
followed by a sequence of decimal digits, because the “^” anchor is missing.
Second, *in general*, string values need to be escaped if they are used in
regular expressions to match substrings literally. For example, “.” needs
to be escaped as ”\.” to match a literal “.” character; otherwise it would
match any non-newline character. My jsx.regexp.escape() and
String.prototype.regExpEscape() methods provide this functionality: you can
pass a string and it returns a pattern string that, when passed to the
RegExp constructor, matches the original string. [1]
However, *in this case* it might not be necessary to escape the pattern
string because the examples do not contain RegExp-special characters.
Third, it is, *in general*, not necessary to use regular expressions for
both parts of the pattern, therefore not necessary to escape the variable
part. The test can consist of two parts: one that tests whether the string
starts with a certain substring, and another that tests whether it ends with
a sequence of decimal digits:
if (tmp.indexOf(pre) == 0 && /\d+$/.test(tmp))
{
// …
}
However, note that although the two-part test may suffice *here*, is *not
equivalent* to the original one because it also would find strings of the
form "foo_bar3" if pre == "foo_".
[1] <http://PointedEars.de/wsvn/JSX/trunk/regexp.js>
--
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Filter array of objects for property Andrew Poulos <ap_prog@hotmail.com> - 2016-10-13 09:44 +1100
Re: Filter array of objects for property Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-10-13 00:27 +0100
Re: Filter array of objects for property Andrew Poulos <ap_prog@hotmail.com> - 2016-10-13 12:57 +1100
Re: Filter array of objects for property Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-10-13 11:21 +0100
Re: Filter array of objects for property Bunny Bocarisse <ben.usenet@bsb.me.uk> - 2016-10-13 13:28 +0100
csiph-web