用JavaScript解析文本 [英] Parsing text with Javascript

查看:103
本文介绍了用JavaScript解析文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Javascript解析在文本框中输入的文本-该文本框将结合各种用户生成的变量来创建随机活动.查看示例可能更有意义.一些示例输入可能是:

I'm trying to use Javascript to parse text that has been entered in a text box - which would combine a variety of user-generated variables to create random activities. This might make more sense looking at the example. Some example input might be:

Activity
@Home
@Out

@Home
Read @book for @time
Clean up @room for @time

@Out
Eat at at @restaurant

@book
Enders Game
Lord of the Rings

@room
bedroom
garage
basement

@restaurant
Red Robin
McDonalds
Starbucks

@time
15 minutes
30 minutes
45 minutes
60 minutes

使用磅/和符号来分隔不同的类别.

Pound/and signs would be used to separate different categories.

然后将从给定的输入中随机确定输出,例如:

The output would then be determined randomly from the given input, for example:

在星巴克吃东西." 或者 读指环王60分钟." 或者 清理车库30分钟."

"Eat at Starbucks." or "Read Lord of the Rings for 60 minutes." or "Clean garage for 30 minutes."

这可行吗?似乎应该很简单,但是我不知道从哪里开始.有什么建议吗?

Is this doable? It seems like it should be fairly straightforward, but I do not know where to start. Any suggestions?

谢谢

阿尔伯特

推荐答案

完全没有问题.根据换行符将文本框值拆分为一个数组.然后,一次遍历数组一个元素,将每个部分的值排序为变量.最后,使用JavaScript的随机数生成器随机确定要选择每个组中的哪个.通过将值分配给HTML元素,将其输出给用户.

No issue at all. Split the textbox value into an array based on line break characters. Then, go through the array one element at a time, sorting the values into variables for each section. Finally, use JavaScript's random number generator to randomly determine which of each group to select. Output to the user by assigning the value to an HTML element.

这篇关于用JavaScript解析文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆