Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

多行字符串转二维数组 #85

Open
Sunny-117 opened this issue Nov 3, 2022 · 3 comments
Open

多行字符串转二维数组 #85

Sunny-117 opened this issue Nov 3, 2022 · 3 comments

Comments

@Sunny-117
Copy link
Owner

const str = ` 1 21    3
4 5  6
7   8 9 `;        /* 多行字符串要用反引号 */
var arr = str.split('\n'); /* 根据换行符分割 */
@bearki99
Copy link

const str = ` 1 21    3
4 5  6
7   8 9 `; /* 多行字符串要用反引号 */
var arr = str.split("\n"); /* 根据换行符分割 */
const res = [];
for (const subarr of arr) {
  let line = subarr.split(" ").filter((e) => e !== " " && e !== '');
  res.push(line);
}
console.log(res);

@veneno-o
Copy link
Contributor

const arr = str.split('\n'); /* 根据换行符分割 */
const res = [];
for (const subarr of arr) {
  let line = subarr.trim().split(/\s+/g)
  res.push(line);
}
console.log(res);

@kangkang123269
Copy link

var str = `line1 part1 part2
line2 part1 part2
line3 part1 part2`;

var arr = str.split('\n').map(function(line) {
    return line..trim().split(/\s+/g);
});

console.log(arr);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants