0

I have a string like

"username 234234 some text"

And I would like to devide them in

"username"

"234234"

and "some text"

I tried with split and substring but failed with finding the second space, most often a blank text got returned.

Thank you very much!

Tester3
  • 101
  • 1
  • 2
  • 10

3 Answers3

1

Hope this may help:

let str = "username 234234 some text";
let arr = str.split(" ");
let username = arr[0];
let num = arr[1];
let otherText = arr.slice(2).join(" ");
Ashraf Sada
  • 4,527
  • 2
  • 44
  • 48
Rahul Raval
  • 114
  • 9
0

Try with this regex /(?<first>.+) (?<second>[0-9]+) (?<third>.+)/g

const testString = "username 234234 some text";
const reg = /(?<first>.+) (?<second>[0-9]+) (?<third>.+)/g;
const matches = reg.exec(myString);
console.log(matches[0]); // username
console.log(matches[1]); // 234234
console.log(matches[2]); // some text
Sid
  • 14,176
  • 7
  • 40
  • 48
0

Here is the code for a discord.js project because you used the tag "discord.js":

const content = message.content.split(" ");
console.log(content) // will log the entire message

content = content.slice(0, 1);
console.log(content) // will log you the username

content = content.slice(1, 2);
console.log(content) // will log you the number

content = content.slice(2);
console.log(content) // will log you the text
Gilles Heinesch
  • 2,889
  • 1
  • 22
  • 43