简体   繁体   English

没有正则表达式的字符串中的数字?

[英]Numbers from inside a string without regex?

I'm making a bot for a gaming chatroom with some friends, but I've hit an impasse.我正在和一些朋友一起为游戏聊天室制作一个机器人,但我陷入了僵局。 Is there a reliable way to get numbers from inside a string of text that won't completely break an inexperienced script kiddy's brain?有没有一种可靠的方法可以从一串文本中获取数字,而不会完全破坏没有经验的脚本小子的大脑? Here's the best I've been able to come up with so far, variables simplified slightly for illustration's sake:这是迄今为止我能想到的最好的,为了说明起见,变量稍微简化了:

var k = [0];
function dieRoll(m,n) {
    for(i = 0; i < m; i++) {
        k[i] = Math.floor(Math.random()*n)+1;
    }
}

var m = text[5];
var n = text[7];
if (text === 'roll '+m+'d'+n) {
    dieRoll(m,n)
    console.log(k);
}

The biggest problem as-is is that it's limited to single-digit input.最大的问题是它仅限于个位数输入。

EDIT: Looping through the text looking for integers is exactly the kind of thing I'm looking for.编辑:循环查找整数的文本正是我正在寻找的那种东西。 I don't have much experience with programming, so I probably tend to end up with overly complicated and confusing messes of spaghetti code that would embarrass anyone remotely professional.我没有太多的编程经验,所以我可能最终会得到过于复杂和混乱的意大利面条式代码,这会让任何远程专业人士感到尴尬。 As for the format of the input I'm looking for, "roll [number of dice]d[highest number on the dice]".至于我正在寻找的输入格式,“掷[骰子数]d[骰子上的最高数字]”。 For anyone who doesn't know, it's the notation most tabletop rpgs use.对于不知道的人来说,这是大多数桌面角色扮演游戏使用的符号。 For example, "roll 2d6" for two normal six-sided dice.例如,对于两个普通的六面骰子,“掷 2d6”。

EDIT: It's not that I'm necessarily against regex, I just want to be able to understand what's going on, so that if and when I need to edit or reuse the code it I can do so without going completely insane.编辑:并不是说我一定反对正则表达式,我只是想能够了解发生了什么,以便当我需要编辑或重用代码时,我可以这样做而不会完全发疯。

EDIT: Thank you all very much!编辑:非常感谢大家! split() seems to be exactly what I was looking for! split() 似乎正是我要找的! It'll probably take some trial and error, but I think I'll be able to get her working how she's supposed to this weekend (Yes I call my bots 'she').这可能需要一些试验和错误,但我想我可以让她在这个周末按照她应该的方式工作(是的,我称我的机器人为“她”)。

Basically, you need to look at the format of the input you're using, and identify certain facts about it.基本上,您需要查看您使用的输入的格式,并确定有关它的某些事实。 Here are the assumptions I've taken based on your question.以下是我根据您的问题做出的假设。

1) The "roll" command comes first followed by a space, and 2) After the command, you are provided with dice information in the form x d y . 1) "roll" 命令首先出现,后跟一个空格,并且 2) 在该命令之后,您将获得x d y形式的骰子信息。

Here's something that should work given those constraints:鉴于这些限制,这里有一些应该起作用的东西:

 function getRollParameters(inputCommand) { var inputWords = inputCommand.split(' '); //Split our input around the space, into an array containing the text before and after the space as two separate elements. var diceInfo = inputWords[1]; //Store the second element as "diceInfo" var diceDetails = diceInfo.split('d'); //Split this diceInfo into two sections, that before and after the "d" - ie, the number of dice, and the sides. //assign each part of the dicedetails to an appropriate variable var dice = diceDetails[0]; var sides = diceDetails[1]; //return our two pieces of information as a convenient object. return { "dice": dice, "sides": sides }; } //a couple of demonstrations console.log(getRollParameters("roll 5d8")); console.log(getRollParameters("roll 126d2"));

Effectively, we're first splitting the string into the "command", and the "arguments" - the information we want.实际上,我们首先将字符串拆分为“命令”和“参数”——我们想要的信息。 Then, we split our arguments up using the "d" as a midpoint.然后,我们使用“d”作为中点来拆分我们的论点。 That gives us two numbers - the one before and the one after the d.这给了我们两个数字——一个在 d 之前,一个在 d 之后。 Then we assign those values to variables, and can use them however we like.然后我们将这些值分配给变量,并且可以随心所欲地使用它们。

This obviously won't deal with more creative or flexible inputs, and isn't tested beyond the examples shown but it should be a decent starting point.这显然不会处理更具创意或灵活的输入,并且不会超出所示示例进行测试,但它应该是一个不错的起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM