JS Подстрока между словом и пробелом - PullRequest
0 голосов
/ 10 сентября 2018

У меня есть строка типа

"имя пользователя 234234, некоторый текст"

И я бы хотел разделить их на

"имя пользователя"

"234234"

и "некоторый текст"

Я пытался использовать split и substring, но не смог найти второй пробел, чаще всего возвращался пустой текст.

Большое спасибо!

Ответы [ 3 ]

0 голосов
/ 10 сентября 2018

Надеюсь, это поможет:

let str = "username 234234 some text";
let arr = str.spit(" ");
let username = arr[0];
let num = arr[1];
let otherText = arr.slice(2).join(" ");
0 голосов
/ 10 сентября 2018

Вот код для проекта discord.js, потому что вы использовали тег "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
0 голосов
/ 10 сентября 2018

Попробуйте с этим регулярным выражением /(?<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
...