我正在尝试构建一个将实际数字输出替换为典型#d#(对于熟悉D&D的人来说)的随机化器。我已经弄清楚了其中的大部分内容,但我一辈子都做不了,想出了当数组中有多个#d#项时如何输出它的方法。下面是我正在使用的代码的片段。您会注意到,在#d#部分中,我使用RegEx替换项目,例如:
原始字符串-"bland. Hunger mushroom, makes you hunger for more mushrooms for the next <roll-1d4> hours"
RegEx之后的输出-“蘑菇味道平淡。饥饿的蘑菇使您在接下来的3小时内渴望更多的蘑菇。”
一切正常,我的问题出在这部分:
原始字符串-"like shiitake. <roll-1d4> mushrooms sprout from your body. The grant <roll-1d4> temporary HP if eaten by anyone other than you for the next <roll-1d4> hours."
RegEx之后的输出-“蘑菇的味道像香菇。您体内会生出2蘑菇。如果在接下来的几个小时内被您以外的任何人食用,它们会提供暂时的HP。” -注意“临时HP”和“下一个小时”部分前面的空白。
关于如何解决此问题的任何想法?
Code Snippet here
<!DOCTYPE html>
<html>
<head>
<title>
Random Table Generators
</title>
</head>
<body>
<h1>Clear Screen</h1>
<p>This is so you can clear the screen from all the random tables you rolled. This will make room for more table rolls.</p>
<div>
<input type="button" id="btnSearch" value="Clear Screen" onClick="clearMe();" />
</div>
<h1>Utility</h1>
<p>This is where most of the randomness comes out. You can find books, trinkets, food menues, junk, even random races, classes, or monsters for an Arrow of Slaying that someone finds...</p>
<div>
<input type="button" value="Mushrooms" onClick="getMushrooms();" />
</div>
<p class="clear" id="mushroom"></p>
<!---Begin JavaScript here--->
<script type="text/javascript">
//Mushroom Randomizer
function chooseMushroom()
{
var mushroom = ["bland. Hunger mushroom, makes you hunger for more mushrooms for the next <roll-1d4> hours", "like gold", "like grapes. You can see in total darkness, but bright light blinds you for the next <roll-1d4> hours", "like milk", "like gum. You laugh long and loudly at the next sentence you hear, regardless of the content", "like mushrooms. Your size increases by one category until you take any damage", "like chicken. Add Chaotic to your alignment, even if it's already present for the next <roll-1d4> hours", "like tapioca. You can speak with oozes & puddings, but not with elves for the next <roll-1d4> hours", "bitter. Everything you intend to convey is tinged with sarcasm for the next <roll-1d4> hours", "like rancid celery", "like cheap gin. You take <roll-1d6> points of damage and gain <roll-1d12> temporary HP", "like nothing. It has no taste", "like chalk. You've forgotten everyone's name in your party for the next <roll-1d4> hours", "like copper coins", "like magic", "like chai. You can't sleep today or tomorrow. (this will cause you to gain 2 levels of exhaustion. This effect stacks if you eat more than one of these mushrooms at a time)", "like sulfur, or maybe eggs.", "like tears. You receive a vision from <roll-1d10> minutes in the future", "like tree bark", "like your own tongue", "minty. Any flame you see appears green", "like raw lobster", "like gin. You can see invisible objects and entities for the next <roll-1d4> hours", "like a sweet cake", "like medicine. You gain a form of limited telepathy, which only allows you to know another creatures' opinion of you for the next <roll-1d4> hours", "like the far realm. INDESCRIBABLE", "like oatmeal. You become incredibly sentimental for the next <roll-1d4> hours", "like chamomile. You have the effects of the Sleep spell cast on you", "like your first kiss. You sweat uncontrollably until you ingest another mushroom", "like cotton. You're convinced you have a fly speed of 30 for the next <roll-1d4> hours", "like water. You can't experience intense emotions for the next <roll-1d4> hours", "like bones. You attract undead in a 100 mile radius for the next <roll-1d4> hours", "like vegemite. You are quite unpalatable to purple worms for the next <roll-1d4> hours", "like dirt", "like plastic. For the next 5 minutes you are clearly aware of your true nature as a D&D player character", "like Dwarf", "like shiitake. <roll-1d4> mushrooms sprout from your body. They grant <roll-1d4> temporary HP if eaten by anyone other than you for the next <roll-1d4> hours", "like apple pie. You smell and taste delicious to everything for the next <roll-1d4> hours", "like Dragon Turtle", "like coffee. For 10 minutes, you share all your senses with the last creature to have eaten this type of mushroom", "like ash", "like paper. You suffer a terrible thirst that can only be quenched with magically created water", "like blubber. The hair on your head turns white and starts to grow 1 inch per minute, even if you don't normally have hair for the next <roll-1d4> hours", "like ozone. Your voice takes on a deep an terrifying tone for the next <roll-1d4> hours", "like citrus", "like cilantro. You hear music that portends unfolding events for the next <roll-1d4> hours", "like fertilizer", "like beets. You regain <roll-1d8> HP per hour that you are buried in soil for the next <roll-1d4> hours", "like old leather", "like fabric. If you stare into someone else's eyes for 1 solid hour you can see the nature of their death", "like seaweed", "like soybeans. None of your attacks, magical or otherwise, can be executed unless you state their name first for the next <roll-1d4> hours", "like tree sap", "smoky. You can squeeze through any opening without penalty for the next <roll-1d4> hours", "like cold honey", "like soap. You become increasingly buoyant for the next <roll-1d4> hours", "like beef jerky", "like marshmallow. A large, soft fungal growth sprouts from your body. You may change its shape at-will for the next <roll-1d4> hours", "like green onions. You can hear the song of all mushrooms. A Constitution saving throw is required every 10 minutes not to sing along for the next <roll-1d4> hours", "like watermelon. The next time you take a long rest, you will awaken to find an inanimate fungal clone of your body lying beside you for the next <roll-1d4> hours", "like tobacco", "sour. You can tell the emotional residue on an object by licking it for the next <roll-1d4> hours", "like crickets", "like strawberry. You enter a deathlike state for <roll-1d6> minutes", "like grass", "like sugar. <roll-1d12> of your teeth fall out and are replaced by fangs for the next <roll-1d4> hours", "like mold. You become encased in a fungal layer, improving your AC by 1 and reducing your speed by 10 for the next <roll-1d4> hours", "like poison, but is perfectly safe to eat", "like butter", "like charcoal. You spiders of all kinds are very attracted to you for the next <roll-1d4> hours", "like gingerbread", "like blood. You regain <roll-1d10> HP for every hour you spend in total darkness, but exposure to sunlight causes you to suffer <roll-1d6> damage per minute exposed for the next <roll-1d4> hours", "like popcorn", "zesty. Your speed doubles, but you must move the entire distance when you take your move action for the next <roll-1d4> hours", "like baked beans", "like a fruit gummy. You can be knocked unconscious, but can't be killed by any damage other than fire or acid for the next <roll-1d4> hours", "like apples. You cannot tell a lie for the next <roll-1d4> hours", "like Kobold pie", "like escargot. You walk at half speed, but can crawl at full speed for the next <roll-1d4> hours", "like BBQ. Eating this mushroom will kill you in <roll-3d20> years"];
var randomMushroom0 = pickRandomMessage(mushroom);
return randomMushroom0;
}
function getMushrooms()
{
var randMushroom = "The mushroom tastes " + chooseMushroom() + "." + "<br>";
document.getElementById("mushroom").innerHTML += randMushroom;
return randMushroom;
}
function pickRandomMessage(messagePool){
var messageIndex = rand(0, messagePool.length-1);
var message = messagePool[messageIndex];
var rollPlaceholder = message.match(/<roll\-([1-9][0-9]*)d([1-9][0-9]*)>/);
if (rollPlaceholder !== null) {
var die = parseInt(rollPlaceholder[1], 10);
var sides = parseInt(rollPlaceholder[2], 10);
var rollTotal = 0;
for (var i = 0; i < die; i++) {
rollTotal += rand(1, sides);
}
message = message.replace(rollPlaceholder[0], rollTotal);
}
return message;
}
function rand(min, max) {
min = parseInt(min, 10);
max = parseInt(max, 10);
if (max < min) {
var temp = max;
max = min;
min = temp;
}
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function clearMe()
{
var cleared = document.getElementsByClassName("clear");
var i;
for (i = 0; i <= cleared.length-1; i++) {
cleared[i].innerHTML = " ";
}
}
</script>i
</body>
</html>
编辑:-添加了实际代码,而不是外部链接。我还尝试了
g
标志,但是所有要做的就是使第一卷输出为0,其余输出为空白。 最佳答案
您可以在正则表达式中使用g
标志,这将使其与字符串中的所有匹配项匹配(不仅是第一个匹配项)。然后,作为第二个参数,为其提供一个函数,该函数每次将返回一个随机数。
编辑
由于您的正则表达式具有两个捕获组,因此您可以访问它们匹配的组:
参数1:完整匹配的字符串(<roll-1d4>
)
参数2:第一个捕获组(1
)
参数3:第二个捕获组(4
)
let message = "like shiitake. <roll-1d4> mushrooms sprout from your body. The grant <roll-2d6> temporary HP if eaten by anyone other than you for the next <roll-1d4> hours."
const regex = /<roll\-([1-9][0-9]*)d([1-9][0-9]*)>/g
message = message.replace(regex, (match,num1,num2) =>{
console.log("num1 =", num1);
console.log("num2 =", num2);
let rand = Math.floor(1 + num1 * Math.random() * num2);
console.log("Dice draw result = ", rand)
return rand
})
console.log(message)
关于javascript - 多个“在线”卷,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55186482/