将引号内的内容计算为一个参数

Count content inside quotes as one argument(将引号内的内容计算为一个参数)
本文介绍了将引号内的内容计算为一个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在使用 node.js 的 discord.js 模块制作一个不和谐的机器人,我有一个基本的参数检测系统,它使用空格作为分隔符并将不同的参数放在一个数组中,这是我的代码的重要部分.(我使用 module.exports 使用单独的命令模块).

const args = message.content.slice(prefix.length).split(/+/);const commandName = args.shift().toLowerCase();常量命令 = client.commands.get(commandName) ||client.commands.find(cmd => cmd.alias && cmd.alias.includes(commandName));

例如当有人写 !give foo 50 bar 时.args 数组是 ['foo','50','bar']
我希望 args 在命令中不分割命令的引用部分,这意味着如果有人写 !give "foo1 bar1" 50 "foo2 bar2".我希望 args 返回这样的数组 ['foo1 bar1', '50','foo2 bar2'] 而不是 ['foo1','bar1','50','foo2','bar1',]

解决方案

这可能不是最有效的方法,但我认为它是可以理解的:我们可以扫描每个字符并跟踪我们是否在里面或在几个引号之外,如果是这样,我们忽略空格.

function parseQuotes(str = '') {让当前 = '',arr = [],inQuotes = 假for (let char of str.trim()) {if (char == '"') {//切换 inQuotes 的值inQuotes = !inQuotes} else if (char == ' ' && !inQuotes) {//如果引号之间有空格并且where'不在,则推入一个新单词arr.push(当前)当前=''} 别的 {//将字符添加到当前单词当前 += 字符}}//压入最后一个词arr.push(当前)返回 arr}//例子//运行代码片段以在控制台中查看结果//!give "foo1 bar1" 50 "foo2 bar2"让 args1 = parseQuotes('!give "foo1 bar1" 50 "foo2 bar2"')console.log(`测试 1: !give "foo1 bar1" 50 "foo2 bar2"
-> [ "${args1.join('", "')}" ]`)//!cmd foo bar baz让 args2 = parseQuotes('!cmd foo bar baz')console.log(`测试 2: !cmd foo bar baz
-> [ "${args2.join('", "')}" ]`)//!cmd foo1 weir"d quot"es "未关闭let args3 = parseQuotes('!cmd foo1 weir"d quot"es "not closed')console.log(`测试 3: !cmd foo1 weir"d quot"es "未关闭
-> [ "${args3.join('", "')}" ]`)

I'm making a discord bot with node.js's discord.js module, I have a basic arguments detection system that uses the whitespace as the separator and puts the different arguments in an array, here's the important part of my code. (I'm using separate command modules using module.exports).

const args = message.content.slice(prefix.length).split(/ +/);
const commandName = args.shift().toLowerCase();
const command = client.commands.get(commandName) || client.commands.find(cmd => cmd.alias && cmd.alias.includes(commandName));

So for example when someone writes !give foo 50 bar. the args array is ['foo','50','bar']
I want the args to not divide quoted parts of the command in the command, meaning if somenoe writes !give "foo1 bar1" 50 "foo2 bar2". I want the args to return an array like this ['foo1 bar1', '50','foo2 bar2'] instead of ['foo1','bar1','50','foo2','bar1',]

解决方案

This may not be the most efficient way to do it, but I think it's comprehensible: we can scan every character and we keep track of whether we're inside or outside a couple of quotes and, if so, we ignore the spaces.

function parseQuotes(str = '') {
  let current = '',
    arr = [],
    inQuotes = false
    
  for (let char of str.trim()) {
    if (char == '"') {
      // Switch the value of inQuotes
      inQuotes = !inQuotes
    } else if (char == ' ' && !inQuotes) {
      // If there's a space and where're not between quotes, push a new word
      arr.push(current)
      current = ''
    } else {
      // Add the character to the current word
      current += char
    }
  }
      
  // Push the last word
  arr.push(current)
    
  return arr
}

// EXAMPLES
// Run the snippet to see the results in the console

// !give "foo1 bar1" 50 "foo2 bar2"
let args1 = parseQuotes('!give "foo1 bar1" 50 "foo2 bar2"')
console.log(`Test 1: !give "foo1 bar1" 50 "foo2 bar2"
-> [ "${args1.join('", "')}" ]`)

// !cmd foo bar baz
let args2 = parseQuotes('!cmd foo bar baz')
console.log(`Test 2: !cmd foo bar baz
-> [ "${args2.join('", "')}" ]`)

// !cmd foo1 weir"d quot"es "not closed
let args3 = parseQuotes('!cmd foo1 weir"d quot"es "not closed')
console.log(`Test 3: !cmd foo1 weir"d quot"es "not closed
-> [ "${args3.join('", "')}" ]`)

这篇关于将引号内的内容计算为一个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)