【问题标题】:Javascript - How should I get values from inbetween two quotes?Javascript - 我应该如何从两个引号之间获取值?
【发布时间】:2018-07-01 08:16:45
【问题描述】:

好的,所以我将 Node.JS 用于不和谐机器人。我正在尝试从引号之间获取值。这是一个例子-

!move "第一频道" "第二频道"

引号之间的值可能会发生变化。我想将这些值存储在一个变量中,以便在其他代码行中使用。

【问题讨论】:

  • 您的意思是要区分“一”和“二”?

标签: javascript node.js discord discord.js


【解决方案1】:

您可以使用正则表达式遍历字符串并捕获您想要的值:

let strValue = '!move "Value one" "Value two" "Value three" "Value four"';
let regex = /"([^"]*)"/g;
let currentResult;
let results = [];
while ((currentResult = regex.exec(strValue)) !== null) {
    results.push(currentResult[1]);
}
// results: ["Value one", "Value two", "Value three", "Value four"]

【讨论】:

  • 如果我想传入"Value \"one\""怎么办?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-09-26
  • 1970-01-01
  • 2013-02-28
  • 2016-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多