亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

金|Node.js - 將字符串劃分為考慮引號的參數

金|Node.js - 將字符串劃分為考慮引號的參數

桃花長相依 2022-09-16 21:59:52
我正在編寫一個不和諧的機器人。Discord是一個帶有聊天功能的社交平臺,您可以在那里編寫機器人代碼。為了觸發機器人命令,機器人會讀取發送到聊天的每條消息。它以繩子的形式發送給他。通過使用這個:機器人將每個單詞分離成一個數組?,F在,我可以這樣做:var args = msg.content.split(' ');if (args[0] === '!command') { //code }我的機器人將跟蹤英雄聯盟玩家。我希望能夠輸入名稱并添加跟蹤原因。所有這些都將進入數據庫。起初,這似乎很簡單,我可以這樣做:if (args[0] === '!command') {  var player = args[1];  var reason = args[2];}現在,如果我發送機器人將正確。!command player1 reasons問題是,在英雄聯盟中,允許在昵稱中使用空格。同時,原因只有一個詞可能不足。如果你試圖這樣做:機器人不會得到作為args[1],而是,將是args[1],并且將是args[2]。同時,現在將是參數[3]而不是參數[2]。!command "player one" reasonsplayer one"playerone"reasons有沒有一種簡單的方法來告訴javascript忽略引號內的空格,這樣它就不會在那里拆分字符串?我可以使用不同的字符來拆分字符串,但是編寫這樣的命令感覺很奇怪,并且是補丁而不是實際的解決方案。!command-player-reasons there
查看完整描述

4 回答

?
森林海

TA貢獻2011條經驗 獲得超2個贊

更新:

因為OP似乎對語言不是很有經驗,所以我選擇提供一個易于閱讀的答案。正確的方法是使用正則表達式捕獲組:https://stackoverflow.com/a/18647776/16805283


這將檢查“message.content”中是否有任何引號,并更改獲取 args 數組的方式。如果未找到引號,它將回退到您自己的代碼以生成 args 數組。請記住,這只有在“message.content”上正好有2個**引號時才有效,因此不應在原因中使用引號

// Fake message object as an example

const message = {

  content: '!command "Player name with a lot of spaces" reason1 reason2 reason3'

};


const { content } = message; 

let args = [];

if (content.indexOf('"') >= 0) {

  // Command

  args.push(content.slice(0, content.indexOf(' ')));

  // Playername

  args.push(content.slice(content.indexOf('"'), content.lastIndexOf('"') + 1));

  // Reasons

  args.push(content.slice(content.lastIndexOf('"') + 2, content.length));

} else {

  args = content.split(' ');

}

// More code using args

如果你想要不帶引號的玩家名稱:


playerName = args[1].replace(/"/g, '');`


查看完整回答
反對 回復 2022-09-16
?
守著一只汪

TA貢獻1872條經驗 獲得超4個贊

你可以在這里使用正則表達式:


const s = `!command "player one" some reason`;


function getArgsFromMsg1(s) {

  const args = []

  if ((/^!command/).test(s)) {

    args.push(s.match(/"(.*)"/g)[0].replace(/\"/g, ''))

    args.push(s.split(`" `).pop())

    return args

  } else {

    return 'not a command'

  }


}


// more elegant, but may not work

function getArgsFromMsg2(s) {

  const args = []

  if ((/^!command/).test(s)) {

    args.push(...s.match(/(?<=")(.*)(?=")/g))

    args.push(s.split(`" `).pop())

    return args

  } else {

    return 'not a command'

  }

}


console.log(getArgsFromMsg1(s));

console.log(getArgsFromMsg2(s));


查看完整回答
反對 回復 2022-09-16
?
月關寶盒

TA貢獻1772條經驗 獲得超5個贊

我建議你單獨解析命令和它的參數。它將承認編寫更靈活的命令。例如:


var command = msg.content.substring(0, msg.content.indexOf(' '));

var command_args_str = msg.content.substring(msg.content.indexOf(' ') + 1);

switch(command) {

    case '!command':

        var player = command_args_str.substring(0, command_args_str.lastIndexOf(' '));

        var reason = command_args_str.substring(command_args_str.lastIndexOf(' ') + 1);

        break;

}


查看完整回答
反對 回復 2022-09-16
?
瀟瀟雨雨

TA貢獻1833條經驗 獲得超4個贊

這是可能的,但對于機器人的用戶來說,只使用不同的字符來拆分兩個參數可能不那么復雜。


例如 如果您知道(或 ,等)不是有效用戶名的一部分。!command player one, reasons,|=>


如果您只支持用戶名部分的引號,則更容易:


const command = `!command`;

const msg1 = {content: `${command} "player one" reasons ggg`};

const msg2 = {content: `${command} player reasons asdf asdf`};


function parse({content}) {

  if (!content.startsWith(command)) {

    return; // Something else

  }

  content = content.replace(command, '').trim();

  const quotedStuff = content.match(/"(.*?)"/);

  if (quotedStuff) {

    return {player: quotedStuff[1], reason: content.split(`"`).reverse()[0].trim()};

  } else {

    const parts = content.split(' ');

    return {player: parts[0], reason: parts.slice(1).join(' ')};

  }

  console.log(args);

}


[msg1, msg2].forEach(m => console.log(parse(m)));


查看完整回答
反對 回復 2022-09-16
  • 4 回答
  • 0 關注
  • 190 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號