久久久久久久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 服務器時的歡迎消息)
主站蜘蛛池模板: 欧美片网站免费 | 国产精品夜夜春夜夜爽久久电影 | 亚洲福利视频一区二区 | 七七婷婷婷婷精品国产 | 国产精品99| 国产精品99久 | 国产中文一区二区三区 | 99re在线视频 | 999国产视频 | 国产精品不卡一区 | 精品日本中文字幕 | 国产精品毛片一区二区三区 | 久久av资源网 | 日产精品久久久一区二区福利 | 色婷婷av777| 精品国产欧美 | 亚洲精品一区二区 | 亚洲高清在线视频 | 国内精品久久影院 | 亚洲精品第一国产综合野 | av在线免费观看网站 | 精品国产乱码久久久久久图片 | 久久精品中文字幕 | 欧美www在线| 精品久久久一区 | 日韩在线一区二区三区 | 色婷婷综合久久久中文字幕 | 欧美中文在线 | 国产精品视频一区二区三 | xxxxx免费视频| 精品视频一区二区三区在线观看 | 久久久久精 | 在线观看日本网站 | 日韩中文视频 | 日韩一级 | 中文字幕三区 | 亚洲国产免费 | 欧美激情一区二区三区 | 精品视频在线观看 | 高清成人免费视频 | 久久精品色视频 |