Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 802107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:32:56+00:00 2026-05-14T23:32:56+00:00

I have the following: [list] [*] test [*] test [*] test [/list] and I

  • 0

I have the following:

[list]
[*] test
[*] test
[*] test
[/list]

and I would like to create a regular expression that turns that into:

<ul>
<li>test</li>
<li>test</li>
<li>test</li>
</ul>

I know regex enough to replace simple tags, but in this case I need to replace li tags only if they are contained inside ul. Is there a way to check that before replacing?

I am using JavaScript if that matters.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:32:57+00:00Added an answer on May 14, 2026 at 11:32 pm

    Given the text:

    [*] test1
    
    [list]
    [*] test2
    [*] test3
    [*] test4
    [/list]
    
    [*] test5
    

    the regex:

    \[\*]\s*([^\r\n]+)(?=((?!\[list])[\s\S])*\[/list])
    

    matches only [*] test2, [*] test3 and [*] test4. But if the [list]‘s can be nested, or a more broader set of a BB-like language needs to be parsed, I opt for a proper parser.

    To do the replacements, replace the regex I suggested with:

    <li>$1</li>
    

    and then replace [list] with <ul> and [/list] with </ul> (assuming [list] and [/list] are only used for lists and are not present in comments or string literals or something).

    When running the following snippet:

    var text = "[*] test1\n"+
        "\n"+
        "[list]\n"+
        "[*] test2\n"+
        "[*] test3\n"+
        "[*] test4\n"+
        "[/list]\n"+
        "\n"+
        "[*] test5\n"+
        "\n"+
        "[list]\n"+
        "[*] test6\n"+
        "[*] test7\n"+
        "[/list]\n"+
        "\n"+
        "[*] test8";
    
    print(text + "\n============================");
    text = text.replace(/\[\*]\s*([^\r\n]+)(?=((?!\[list])[\s\S])*\[\/list])/g, "<li>$1</li>");
    text = text.replace(/\[list]/g, "<ul>");
    text = text.replace(/\[\/list]/g, "</ul>");
    print(text);
    

    the following is printed:

    [*] test1
    
    [list]
    [*] test2
    [*] test3
    [*] test4
    [/list]
    
    [*] test5
    
    [list]
    [*] test6
    [*] test7
    [/list]
    
    [*] test8
    ============================
    [*] test1
    
    <ul>
    <li>test2</li>
    <li>test3</li>
    <li>test4</li>
    </ul>
    
    [*] test5
    
    <ul>
    <li>test6</li>
    <li>test7</li>
    </ul>
    
    [*] test8
    

    A small explanation might be in order:

    • \[\*]\s* matches the sub string [*] followed by zero or more white space characters;
    • ([^\r\n]+) gobbles up the rest of the line and saves it in match group 1;
    • (?=((?!\[list])[\s\S])*\[/list]) ensures that every match group 1 must have a sub string [/list] ahead of without encoutering a [list]

    EDIT

    Or better yet, do as Gumbo suggest in the comment to this answer: match all [list] ... [/list] and then replace all [*] ... in those.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.