-
-
Notifications
You must be signed in to change notification settings - Fork 238
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
Comments
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); |
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); |
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); |
const str = ` 1 21 3
4 5 6
7 8 9 `; /* 多行字符串要用反引号 */
var arr = str.split("\n"); /* 根据换行符分割 */
let result = [];
let reg = /\d+/g;
for (let i = 0; i < arr.length; i++) {
let data = arr[i].match(reg);
result.push(data.map((e) => +e)); /* 转为数字并添加到结果数组 */
// let data = arr[i].trim().split(" ");
// result.push(data.filter((e) => e != ""));
}
console.log(result); /* 打印结果 */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The text was updated successfully, but these errors were encountered: