【问题标题】:Finding something in between characters在字符之间找到一些东西
【发布时间】:2022-09-23 02:01:22
【问题描述】:

我怎样才能得到引号之间的单词,然后用从文件中读取的其他内容替换它

注意:我知道如何从文件中读取和写入,我只需要知道如何从引号内获取单词

  • 你知道正则表达式吗?

标签: javascript


【解决方案1】:

如果您知道要查找的单词:

let text = 'Sentence with "word" in it';
let wordFromFile = "nothing";
let newText = text.replace('"word"', wordFromFile);

否则正则表达式很有用。

【讨论】:

    【解决方案2】:

    使用Regex(正则表达式)regex replace

    let str = 'How can I get the word in between "quotation marks"';
    let word = str.replace(/.*"(.*?)".*/g,'$1');
    let replacedWord = 'single quotes';
    console.log(str.replace(word,replacedWord))

    【讨论】:

      猜你喜欢
      • 2015-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-06
      • 1970-01-01
      • 2020-09-29
      相关资源
      最近更新 更多