Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #25110
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Cannot manipulate array produced with string.match |
| Date | 2014-06-28 23:53 +0200 |
| Organization | PointedEars Software (PES) |
| Message-ID | <1892446.4VnW0yGc5p@PointedEars.de> (permalink) |
| References | (1 earlier) <lojfg6$i56$2@dont-email.me> <lokbfi$vac$1@dont-email.me> <lokfoe$q6k$1@dont-email.me> <lokjjc$o0v$1@dont-email.me> <loknef$inh$1@dont-email.me> |
SteveYoungTbird wrote:
> On 06/27/2014 10:18 PM, SteveYoungTbird wrote:
>> On 06/27/2014 09:13 PM, Denis McMahon wrote:
>>> On Fri, 27 Jun 2014 20:00:18 +0200, SteveYoungTbird wrote:
>>>> I did as you suggested and strangely the alert reads " 'jim' !== 'jim'
>>>> " as well as all of the other names not being equal to 'jim'
>>> Hmm, now you need to figure out what the differences are.
>
> Got it! arr.match produces a string with a space after the comma.
s/arr/string/
> Therefore 'jim' in the loop was actually ' jim' and therefore didn't
> match,
It produces that because you have /(?:^| )/ in
>>>>>> var arr = [];
^^^^^[1][2]
>>>>>> arr = string.match(/(?:^| )([a-zäöüß]+)(?= [A-Z])/g);
[3]^^^^^^^^[4]-------^^^^^^^^^^^[5]
The /(?:…)/ only prevents capturing; it is _not_ a zero-length lookbehind
(/(?<!…)/ would be, but it is not supported in ECMAScript as yet).
Many parts of this code do not make sense:
[1] These languages use dynamic type-checking. You do not need to
initialize a variable that you will assign a reference to an Array
instance with a reference to an Array instance.
[2] No additional initialization is necessary. String.prototype.match()
always returns a reference to an Array instance if successful
(otherwise “null” which overwrites the previous value anyway).
[3] /(?:^| )…/g does not make sense, see above.
[4] Capturing the sequence of word characters does not make sense with
the global flag, as the resulting Array only contains the matches
as primitive string values, if any.
[5] /…(?= [A-Z])/g (zero-length positive lookahead) does not appear to
make sense when you want to match/filter words. Why would you only
match words that precede a word starting with a capital letter?
What about multiple spaces, tabs, and newlines?
In order to match words, simply match consecutive word characters (for the
non-word characters delimit words and are not matched):
var arr = string.match(/[a-zäöüß]+/ig);
JSX:regexp.js can help there with basic support for Unicode character
properties, like \p{L} for all letters. But if you want to use .match()
instead of .exec() then, you need to cast the String to a jsx.regexp.String:
/*
* ["Über", "sprießende", "Äcker", "liefen", "zwölf", "Hühner",
* "sagte", "der", "Bär", "in", "der", "Öffentlichkeit"]
*/
new jsx.regexp.String(
"Über sprießende Äcker liefen zwölf Hühner, sagte der Bär"
+ " in der Öffentlichkeit."
).match(new jsx.regexp.RegExp("\\p{L}+", "g"))
(You could say that this sentence – that just came to my mind – is an umlaut
pangram, as a proof-of-concept. It also features the sz ligature, used as
in non-reformed Federal German spelling.)
See also:
- <http://PointedEars.de/scripts/test/regexp>
- <http://stackoverflow.com/a/8568325/855543>
- My sig for the source code.
In order to filter out words, simply match on them or replace them. There
is no need for array iteration.
That said, Array.prototype.filter() (ES 5+) and equivalents are more
efficient here than multiple Array.prototype.splice(), even though you
cleverly iterate in reverse order so that the changed length does not
matter.
Please post using your real name.
--
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
Cannot manipulate array produced with string.match SteveYoungTbird <stephen.young@chello.at> - 2014-06-27 09:17 +0200
Re: Cannot manipulate array produced with string.match Denis McMahon <denismfmcmahon@gmail.com> - 2014-06-27 10:02 +0000
Re: Cannot manipulate array produced with string.match SteveYoungTbird <stephen.young@chello.at> - 2014-06-27 20:00 +0200
Re: Cannot manipulate array produced with string.match Denis McMahon <denismfmcmahon@gmail.com> - 2014-06-27 19:13 +0000
Re: Cannot manipulate array produced with string.match SteveYoungTbird <stephen.young@chello.at> - 2014-06-27 22:18 +0200
Re: Cannot manipulate array produced with string.match SteveYoungTbird <stephen.young@chello.at> - 2014-06-27 23:24 +0200
Re: Cannot manipulate array produced with string.match Denis McMahon <denismfmcmahon@gmail.com> - 2014-06-28 01:07 +0000
Re: Cannot manipulate array produced with string.match Denis McMahon <denismfmcmahon@gmail.com> - 2014-06-28 01:16 +0000
Re: Cannot manipulate array produced with string.match Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-06-28 23:53 +0200
Re: Cannot manipulate array produced with string.match SteveYoungTbird <stephen.young@chello.at> - 2014-06-29 22:24 +0200
Re: Cannot manipulate array produced with string.match Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-06-29 23:19 +0200
Re: Cannot manipulate array produced with string.match JJ <jj4public@vfemail.net> - 2014-06-27 19:31 +0700
csiph-web