0

我有一个字符串

“用户名 234234 一些文字”

我想把它们分成

“用户名”

“234234”

和“一些文字”

我尝试使用拆分和子字符串,但找不到第二个空格,通常返回空白文本。

非常感谢!

4

3 回答 3

1

希望这可能会有所帮助:

let str = "username 234234 some text";
let arr = str.split(" ");
let username = arr[0];
let num = arr[1];
let otherText = arr.slice(2).join(" ");
于 2018-09-10T12:09:31.050 回答
0

试试这个正则表达式/(?<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
于 2018-09-10T12:01:01.397 回答
0

这是一个 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
于 2018-09-10T12:54:12.110 回答