用正则表达式解析带标记的输出的Java脚本

时间:2019-03-11 05:17:14

标签: javascript jquery

我正在尝试从游戏中解析此telnet输出。我尝试使用正则表达式,但它不会捕获任何内容,并始终返回null。我从搜索中尝试了几个不同的示例,但是没有用。下面是我的字符串,我想在 之间捕获,稍后我想捕获所有 <项目... > 标签中。我已将 str 的输出发送到控制台,并且所有输出均按预期显示。这是我当前正在尝试的正则表达式,它返回null。

    var result = str.match( /<motd>(.|\n|\r\n)*?<\/motd>/g );
    console.log(result);

和str:

    <motd>
    Welcome to MUD

    ***************
    UPDATE May 11, 2018 Interface v31.2
    ***************
    NEW INTERFACE: v31.2 is online!

    Changes to INTERFACE:
    Larger Message Queue
    Built-in, auto-synced Tick Timer.
    Junk/Sell/Drop on multiple items now works for entire stack of items.
    New Mob Art
    Ungroup Button fixed.

    ****
    NEW EDITABLE COMMAND ENTRY BOX!!
    ****

    Changes to GAME:
    Miscellaneous small fixes to make the interface function cleaner.
    Shop and Inventory Parsing bugs (overflow) improved heavily.
    New items, it's a surprise. Hint: Think Orc Dreams.
    Fixed small bugs in room art.
    New character creation bug (chars being reset to level 1) fixed.
    Score made more detailed on enchantments.
    Large quantity junking fixed.
    Follow Spam Removed. You're welcome. You may grovel now.
    Stealing from newbies fixed. Thanks to the guy who reported that!
    Limbo bugs fixed, including not being able to idle out.
    Quicklevelling fixed. We don't recommend you make this code tell us what you're doing.
    Dark showing properly on various spells/light/sleep/combat changes.
    Orc jail meat collector fixed.
    </motd>
    <paiddays 0>
    <players>
    <item Time 1 ~ 1409 0 1 150532 171205 171959 150801 170762 152159 2556 1453 170023 0>
    <item Mep 1 ~ 1509 0 1 555 0 1959 0 750 2153 2555 0 4 0>
    <item ZugZug 1 ~ 9025 3 1 551 1210 142010 803 140750 142150 2550 171453 145 0>
    <players>
    <playername>

2 个答案:

答案 0 :(得分:0)

使用DOM解析器。与您的问题最接近的是JQuery DOM解析器。

var myOut = $.parseXML(str);   

var items = $(myOut).find('item');

答案 1 :(得分:0)

尝试正则表达式:

/<motd>[^<>]*?<\/motd>/igm

这里:

  1. i:要忽略大小写
  2. g:用于全局搜索
  3. m:用于多行 匹配