繁体   English   中英

使用javascript中的正则表达式无法获得正确的输出

[英]Not getting proper output with Regular expression in javascript

我有一句话,我需要提取它并获得正确的输出。

Welcome to project, are you a here(1. new user one, 2. test user two, 3. minor Accident one or 4. Major Accident)

我现在使用正则表达式获取输出:

Welcome to project, are you a here
1. new user
2. test user
3. minor Accident or
4. Major

我应该使用正则表达式按句子获取输出:

Welcome to project, are you a here
1. new user one
2. test user two
3. minor Accident one
4. Major Accident

这是下面的代码,在我使用过的plnker中进行了更新,因为我对形式表示的了解不多,所以没有解决方案。

 $(document).ready(function() { regex = /.+\\(|\\d. [\\w\\s]+ /g; maintext = "Welcome to project, are you a here(1. new user one, 2. test user two, 3. minor Accident one or 4. Major Accident)"; matches = maintext.match(regex); text_split0 = matches[0].slice(0, -1); text_split1 = matches[1]; text_split2 = matches[2]; text_split3 = matches[3]; text_split4 = matches[4]; console.log(text_split0); console.log(text_split1); console.log(text_split2); console.log(text_split3); console.log(text_split4); $(".messages").append('<li>' + text_split0 + '</li><li>' + text_split1 + '</li><li>' + text_split2 + '</li><li>' + text_split3 + '</li><li>' + text_split4 + '</li>'); // $("li:contains('undefined')").remove() }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul class="messages"> </ul> 

而不是创建一个独角兽正则表达式; 分两步完成。 将正则表达式简化为仅查找数字。 因此,您正在与此匹配:

https://regex101.com/r/BXf1IM/1

"1. new user one, 2. test user two, 3. minor Accident one or 4. Major Accident"

然后,用数字分割该字符串会更容易。

list.split(/([\d]. )/g)

然后,您可以遍历这些内容以将其添加到订单列表中。

<ol class="messages">
    <li>new user one</li>
    <li>test user two</li>
    <li>minor Accident one or </li>
    <li>Major Accident</li>
</ol>

您需要清理第三项上的“或”。

完整脚本:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<ul class="messages"></ul>
<script>
    var text = 'Welcome to project, are you a here(1. new user one, 2. test user two, 3. minor Accident one or 4. Major Accident)';
    var regex = /\((.*)\)/;
    var m;

    if ((m = regex.exec(text)) !== null) {
        console.log(m[1]);
        // 1. new user one, 2. test user two, 3. minor Accident one or 4. Major Accident
        var list = m[1];

        var items = list.split(/([\d]. )/g);

        var message = $('.messages');



        // Remove ' or ' from secondLastItem by removing the last 4 characters
        var secondLastItem = items[items.length-3];
        items[items.length-3] = secondLastItem.substring(0, secondLastItem.length - 4);

        for(var i=1;i<items.length;i++) {
            // Ordered list
            // message.append('<li>' + items[++i].replace(', ', '') + '</li>');
            // Unordered list
            message.append('<li>' + items[i] + items[++i].replace(', ', '') + '</li>');
        }
    }

    // The intro text
    var introText = text.split('(');
    console.log(introText[0]);
</script>

暂无
暂无

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

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