2

I want to split text which was provided by user into single characters and then edit them.
I used RegEx to divide whole sentence into words and then into characters, but finally I got array of arrays :/

Code (check console):

function showLetters() {
  const simpleText = 'Hello my friends';
  let words = simpleText.split(/\W+/),
      letters = words.map(word => {
        return word.match(/\S/g).join();
      });

  console.log(letters);
}

showLetters();

I want to know how can I get to this letters in finall array.
I beg for help, i'm newbie

Bembnias
  • 21
  • 3
  • 2
    Can you update the question with an expected input output combination? – Nitheesh Mar 16 '20 at 12:01
  • 1
    _“I want to know how can I get to this letters in finall array”_ - so your question is basically, how do I access elements in a JavaScript array? Then you should probably be reading up on basics like that in some beginner’s tutorial right now, rather than come asking here …? This site is not really a teaching ground for such basics. – CBroe Mar 16 '20 at 12:04
  • Does this answer your question? [How do you get a string to a character array in JavaScript?](https://stackoverflow.com/questions/4547609/how-do-you-get-a-string-to-a-character-array-in-javascript) – Sadaf Niknam Mar 16 '20 at 12:06
  • @CBroe when i said finall i meant that i got arrays in array i know how to loop through standard array... – Bembnias Mar 16 '20 at 19:34

5 Answers5

2

RegEx isn't needed here I don't think, if I understand you correctly this is what you need:

function showLetters() {
  const simpleText = "Hello my friends";
  let letters = simpleText.split("").filter(letter => letter !== " ");

  console.log(letters);
}

showLetters();

If you want the 'Blank Spaces' to remain as entities within the array, just remove the .filter part.

Inch High
  • 835
  • 5
  • 17
1

Just split on an optional white space, which will return all letters except white space.

function showLetters() {
  const simpleText = 'Hello my friends';
  let letters = simpleText.split(/\s?/);
  console.log(letters);
}

showLetters();
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • Thank you, but there's another question, how can i join letters from this array into words i had on beginning – Bembnias Mar 16 '20 at 19:37
  • Can you keep the original sentence? What happens after you put the letters in an array? Are they modified in any way? – Ja͢ck Mar 16 '20 at 23:49
0

Might it not be simpler to regex replace all nonword chars with nothing, then split on nothing (Or use Array.from(string))?

str.replace(/\W/gi, "").split("");
Caius Jard
  • 72,509
  • 5
  • 49
  • 80
0

Try this:

function showLetters() {
  const simpleText = 'Hello my friends';
  let words = simpleText.split("");

  console.log(words);
}

showLetters();

You'll get every character including white space in words array, so when you join them using .join("") you'll get words separated by white space.

Devendra
  • 119
  • 1
  • 5
0

Try This

<html>
    <body>
    
    <button onclick="showLetters()">Try it</button>

    <p id="demo"></p>

        <script>

            function showLetters() {

            var simpleText = "Hello my friends";
            var letters  = simpleText.split("");

            document.getElementById("demo").innerHTML = letters;
            
            }

        </script>

</body>

</html>
  • Hi! Thanks for your contribution but please make sure that you explain what changes you made and how it affects the existing code. – Ajay Gupta Mar 16 '20 at 15:11