javascript 将字符串从数字点中拆分出来,并在新数组中仅保留点的语句

dfuffjeb  于 2023-02-02  发布在  Java
关注(0)|答案(6)|浏览(136)

我有这个输入

"\n\nOpen Ended Questions:\n1. What makes Vue a popular choice for web development?\n2. How does Vue compare to other JavaScript frameworks?\n3. What are the advantages of using Vue?\n\nClosed Ended Questions:\n1. Does Vue support server-side rendering?\n2. Is Vue compatible with TypeScript?\n3. Does Vue have a built-in router?"

我想要此输出-

[
  "What makes Vue a popular choice for web development?",
  "How does Vue compare to other JavaScript frameworks?",
  "What are the advantages of using Vue?",
  "Does Vue support server-side rendering?",
  "Is Vue compatible with TypeScript?",
  "Does Vue have a built-in router?",
]

"我试过了“

let string = "\n\nOpen Ended Questions:\n1. What makes Vue a popular choice for web development?\n2. How does Vue compare to other JavaScript frameworks?\n3. What are the advantages of using Vue?\n\nClosed Ended Questions:\n1. Does Vue support server-side rendering?\n2. Is Vue compatible with TypeScript?\n3. Does Vue have a built-in router?"

// First, remove all line breaks and two strings
string = string.replace(/(\r\n|\n|\r)/gm, "").replace('Open Ended Questions:', '').replace('Closed Ended Questions:', '');

// Split the string from this format, "<integer><dot><space>"
let result = string.split(/(\d+)\.\ /);

// Filter the only items which are not empty and not a number
result = result.filter(item => item && isNaN(item));

// Final result
console.log(result);

代码说明-

1.首先,我删除了所有的换行符和一些不需要的字符串。
1.其次,我将字符串从<integer><dot><space>中拆分出来,即"1. ", "2. "等。
1.最后,在单独的数组中只过滤数字点的语句。
该解决方案运行良好,但我不确定是否是正确的方法,因为这个硬编码的删除操作-replace('Open Ended Questions:', '').replace('Closed Ended Questions:', '')
有谁能提出一个更好的/不复杂的/正确的方法来做这件事吗?

xpcnnkqh

xpcnnkqh1#

迭代输入中的行,如果一行匹配/^\d+\.\s+(即数字-点-空格),则将该行的其余部分放入数组中:

input = "\n\nOpen Ended Questions:\n1. What makes Vue a popular choice for web development?\n2. How does Vue compare to other JavaScript frameworks?\n3. What are the advantages of using Vue?\n\nClosed Ended Questions:\n1. Does Vue support server-side rendering?\n2. Is Vue compatible with TypeScript?\n3. Does Vue have a built-in router?"

output = []

for (let line of input.split('\n')) {
  line = line.trim()
  let m = line.match(/^\d+\.\s+(.+)/)
  if (m)
    output.push(m[1])
}

console.log(output)

你也可以用一个正则表达式来实现,但是这样的话可读性会差一些。

input = "\n\nOpen Ended Questions:\n1. What makes Vue a popular choice for web development?\n2. How does Vue compare to other JavaScript frameworks?\n3. What are the advantages of using Vue?\n\nClosed Ended Questions:\n1. Does Vue support server-side rendering?\n2. Is Vue compatible with TypeScript?\n3. Does Vue have a built-in router?"

output = [...input.matchAll(/^\d+\.\s+(.+)/gm)].map(r => r[1])

console.log(output)
smtd7mpg

smtd7mpg2#

我假设每个问题都以?结尾,并根据您提到的输入字符串以新行\n开始。考虑到上述模式,以下是实现所需输出的解决方案。

// Input string
const str = "\n\nOpen Ended Questions:\n1. What makes Vue a popular choice for web development?\n2. How does Vue compare to other JavaScript frameworks?\n3. What are the advantages of using Vue?\n\nClosed Ended Questions:\n1. Does Vue support server-side rendering?\n2. Is Vue compatible with TypeScript?\n3. Does Vue have a built-in router?";

// Splitted the input string based on the new line character '\n'
const splittedStr = str.split("\n");

// Filtered out the array items which ends with question mark (?)
const questionsArr = splittedStr.filter(str => str.endsWith('?'));

// Finally removing the numbers from the starting of the question texts.
const res = questionsArr.map(str => str.replace(/^[\d]+\.\ /, ''));

// Desired output
console.log(res);
mwyxok5s

mwyxok5s3#

1.拆分为行
1.过滤管路
1.Map行(删除枚举器)

const input = "\n\nOpen Ended Questions:\n1. What makes Vue a popular choice for web development?\n2. How does Vue compare to other JavaScript frameworks?\n3. What are the advantages of using Vue?\n\nClosed Ended Questions:\n1. Does Vue support server-side rendering?\n2. Is Vue compatible with TypeScript?\n3. Does Vue have a built-in router?"

const lines = input.split('\n');
const listItems = lines
  .filter((line) => /^\d+\./.test(line))
  .map((line) => line.replace(/^\d+\./, "").trim());

console.log(listItems);
7eumitmz

7eumitmz4#

这取决于你的输入,如果问题总是有相同的结构?如果是,你必须定义一个正则表达式,匹配你的问题格式,从那里你不需要替换任何句子与空字符串:

let string = "\n\nOpen Ended Questions:\n1. What makes Vue a popular choice for web development?\n2. How does Vue compare to other JavaScript frameworks?\n3. What are the advantages of using Vue?\n\nClosed Ended Questions:\n1. Does Vue support server-side rendering?\n2. Is Vue compatible with TypeScript?\n3. Does Vue have a built-in router?"
// ([0-9])+ is the question number and it has to be 1 or more than one character
// \. is the dot after question number
// .+ is your question
// (\?)? the first question mark after the question text
let regexp = /([0-9])+\..+(\?)?/g;
var questions = string.match(regexp);
var result = [];
questions.forEach((l) => result.push(l.replace(/([0-9])+\./g,'').trim()));
console.log(result);
eh57zj3b

eh57zj3b5#

使用正后视(?<=)和前视(?=)

const str = "\n\nOpen Ended Questions:\n1. What makes Vue a popular choice for web development?\n2. How does Vue compare to other JavaScript frameworks?\n3. What are the advantages of using Vue?\n\nClosed Ended Questions:\n1. Does Vue support server-side rendering?\n2. Is Vue compatible with TypeScript?\n3. Does Vue have a built-in router?";

const items = str.match(/(?<=^\d+\. +).+?(?=\n|$)/gm)
console.log(items);

说明:Regex101.com

  • lookbehind:(?<=\n\d+\. +)字符串开头、一个或多个数字、点、一个或多个空格
  • 非贪婪匹配.+?任何字符一次或多次,尽可能少
  • 前瞻:(?=\n|$)换行符或行尾

没有lookbehindAssert

使用这个简化的正则表达式,它基本上匹配 *Digit/s后跟点和空格 * 模式的行首^,并捕获行末$之前的剩余字符串(.+)(带有全局和多行标志gm

/^\d\. (.+)$/gm

Regex101.com demo and explanation
支持基于WebKit的浏览器,例如:Safari不支持LookbehindAssert,您可以使用String.prototype.matchAll()获取具有匹配组的迭代器。使用Spread Syntax ...获取2D数组,使用match[1]访问第一个匹配组,并使用Array.ptototype.map()将其转换为Array

const str = "\n\nOpen Ended Questions:\n1. What makes Vue a popular choice for web development?\n2. How does Vue compare to other JavaScript frameworks?\n3. What are the advantages of using Vue?\n\nClosed Ended Questions:\n1. Does Vue support server-side rendering?\n2. Is Vue compatible with TypeScript?\n3. Does Vue have a built-in router?";

const items = [...str.matchAll(/^\d\. (.+)$/gm)].map(m => m[1]);
console.log(items);

要额外支持不支持String.prototype.matchAll()方法的旧Internet Explorer浏览器,请使用.exec(),如下所示:

const str = "\n\nOpen Ended Questions:\n1. What makes Vue a popular choice for web development?\n2. How does Vue compare to other JavaScript frameworks?\n3. What are the advantages of using Vue?\n\nClosed Ended Questions:\n1. Does Vue support server-side rendering?\n2. Is Vue compatible with TypeScript?\n3. Does Vue have a built-in router?";

const reg = /^\d\. (.+)$/gm;
const res = [];
let m;
while(m = reg.exec(str)) res.push(m[1]);
console.log(res);
k4ymrczo

k4ymrczo6#

1.如果您与新产品线分开,您将获得

part  0: 
part  1: 
part  2: Open Ended Questions:
part  3: 1. What makes Vue a popular choice for web development? 
part  4: 2. How does Vue compare to other JavaScript frameworks?
part  5: 3. What are the advantages of using Vue?
part  6: 
part  7: Closed Ended Questions:
part  8: 1. Does Vue support server-side rendering?
part  9: 2. Is Vue compatible with TypeScript?
part 10: 3. Does Vue have a built-in router?

1.然后您可以过滤空行和不相关的行。
1.然后删除开头的数字。

相关问题