javascript - Create array containing the single chars of a string. How does these regular expression work? -
i guess start example code:
var sentence = 'lorem ipsum dolor sit amet, consectetuer adipiscing elit.'; // makes array single chars of sentence elements. sentence = sentence.split(''); // makes multi-dimensional array. var result = sentence.map(function(ch) { // using /./ leads multi-dimensional array too. // each array contains 2 empty strings elements. // => ["", ""] return ch.split(/. /); }); result.foreach(function(item, i) { console.log(item); // [ ["l"], ["o"], ["r"] ... ] });
the purpose clear me: it's making multi-dimensional array. each element got 1 char of sentence.
what don't understand regular expression used.
. stands 1 arbitrary character in regular expressions.
what's purpose of blank after dot ( . ) ?
moreover: if rid of blank , write /./ results arrays too. each array contains 2 empty strings ( "" ) .
that behaviour isn't clear me too.
can explain described regular expression behaviour?
like add: have seen code-snippet here ... https://davidwalsh.name/write-javascript-promises
at bottom of section "chaining".
"x".split(/. /)
has same effect as
"x".split(/rabbit/)
it collect part of string doesn't contain delimiter (=the whole string), , stops.
"x".split(/./)
collects part doesn't match .
(=an empty string), consumes delimiter (=x
) , appends rest of string (which empty) result.
Comments
Post a Comment