【问题标题】:JavaScript method to grab a phrase from a paragraph [duplicate]从段落中获取短语的JavaScript方法[重复]
【发布时间】:2019-04-05 12:24:13
【问题描述】:

所以我有一个字符串数组(段落),每个字符串中的第一个短语代表标题。比如……

[
  "The Lion, the Witch and the Wardrobe \n\ There was once a lion, a 
  witch and a wardrobe. The End"
]

如您所见,标题由换行符\n\ 分隔。有没有一种方法可以用来在换行符之前获取字符串的一部分并将其存储在变量中?

我有大量这样的字符串,所以我正在寻找一个不只是匹配“狮子、女巫和魔衣橱”的解决方案

提前谢谢各位。

我需要的结果...

let headers = [ "狮子、女巫和魔衣橱", "example", "example" ]

let body = ["The was once a...", "example", "example"]

【问题讨论】:

  • 这里的example 是什么?
  • 你尝试了什么?将您的代码添加到问题中。
  • 仅供参考,换行符只是\n(没有最后的反斜杠)

标签: javascript


【解决方案1】:

你可以拆分字符串,然后像这样提取第一个元素:

const phrase = [
  "The Lion, the Witch and the Wardrobe \n\ There was once a lion, a witch and a wardrobe. The End"
]

const getHeader = ([header, ...rest]) => header

const result = getHeader(phrase[0].split('\n'))

console.dir(result)

编辑后,我发现您还想要正文,您可以这样做:

const phrase = [
  "The Lion, the Witch and the Wardrobe \n\ There was once a lion, a witch and a wardrobe. The End"
]

const getHeader = ([header, ...body]) => ({
  header: [header],
  body,
})

const result = getHeader(phrase[0].split('\n'))

console.dir(result)

加上额外的猜测:

从您的问题看来,您有一个文本数组,并且希望每个数组输出两个数组;标题数组和正文数组。这可能是这样的:

const texts = [
  "The Lion, the Witch and the Wardrobe \n\ There was once a lion, a witch and a wardrobe. The End",
  "The Bible\n\In the beginning, God created the heavens and earth"
]

const getHeaderAndBody = ([header, ...body]) => ({
  header: [header],
  body,
});

const mergeHeadersAndBodies = (prev, curr) => ({
  headers: [
    ...(prev.headers || []),
    ...curr.header
  ],
  bodies: [
    ...(prev.bodies || []),
    ...curr.body
  ]
})
  
const splitToNewLines = text => text.split('\n')

const mapToHeadersAndBodies = input => input
  .map(splitToNewLines)
  .map(getHeaderAndBody)
  .reduce(mergeHeadersAndBodies, {})
  
const result = mapToHeadersAndBodies(texts)

console.dir(result)

【讨论】:

    【解决方案2】:
    function myFunction() {
      var str = "The Lion, the Witch and the Wardrobe \n There was once a lion, a \n witch and a wardrobe. The End";
      var res = str.split("\n");
      alert(res);
    }
    

    尝试使用javascript拆分功能。

    【讨论】:

    • 当然这只是对标题和正文发出警告,现在作为一个数组?
    • 这不能回答问题?我假设他有一个相同格式的字符串数组。请参阅我的答案以了解我的意思。
    【解决方案3】:

    只使用数组的map函数和字符串的substring函数

    let body = ["a \n\ b","c \n\ d","e \n\ f"]
    let headers = arrayOfStrings.map(s => s.substring(0,s.indexOf(" \n\ ")))
    

    并且换行符应该只是\n,最后没有\,否则会导致问题

    【讨论】:

      【解决方案4】:

      您可以使用其他人提到的 Javascript 的 split 方法。

      如果您要解析的所有字符串的格式都相同,即 {header, \n, body} 则使用以下代码。它将每个字符串分成两个,然后将每个部分存储在单独的变量中。

      const phrases = [
        "The Lion, the Witch and the Wardrobe \n There was once a lion, a witch and a wardrobe. The End",
        "This is the header \n this is the body",
        "This is another header \n this is another body"
      ]
      
      let headers = []
      let bodies = []
      
      phrases.forEach(phrase => {
        let split = phrase.split('\n')
        headers.push(split[0])
        bodies.push(split[1])
      })
      
      console.log(headers)
      console.log(bodies)

      【讨论】:

        猜你喜欢
        • 2022-12-11
        • 1970-01-01
        • 1970-01-01
        • 2020-10-18
        • 2021-11-25
        • 1970-01-01
        • 2014-08-20
        • 2016-10-23
        • 2012-11-16
        相关资源
        最近更新 更多