久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

將引號內的內容計算為一個參數

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('", "')}" ]`)

這篇關于將引號內的內容計算為一個參數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機器人提及發出該機器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復必須使用導入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務器時的歡迎消息)
主站蜘蛛池模板: 91久久爽久久爽爽久久片 | 精品成人一区 | 麻豆视频在线免费观看 | 亚洲国产aⅴ成人精品无吗 亚洲精品久久久一区二区三区 | 欧美日本一区 | 亚洲国产小视频 | 久久丝袜 | 国产伦精品一区二区三区精品视频 | 断背山在线观看 | 欧美综合色| 国产精品久久久久久久久久久免费看 | 成人免费精品视频 | 国产精品欧美大片 | 日韩在线精品 | 亚洲香蕉| 亚洲精品中文字幕中文字幕 | 精品一二区 | 午夜在线视频一区二区三区 | 韩日三级| 密室大逃脱第六季大神版在线观看 | 欧美一区二区在线播放 | 亚洲视频中文字幕 | 国产一区二 | 特级丰满少妇一级aaaa爱毛片 | 国内成人免费视频 | 久久久久亚洲精品 | 亚洲一区二区三区欧美 | 91精品久久久久久久久中文字幕 | 亚洲精品久久久一区二区三区 | 国产乱码精品一品二品 | 国产麻豆乱码精品一区二区三区 | 一区二区三区亚洲精品国 | 日韩中文字幕视频在线 | 国产精品一区在线观看你懂的 | 蜜桃精品在线 | 精品人伦一区二区三区蜜桃网站 | 国产福利91精品一区二区三区 | 国产一级一片免费播放 | 久草免费在线视频 | 欧美一级三级在线观看 | 精久久久|