SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2006
    Location
    Sweden
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regular expression, BB code

    Hi,
    I'm trying to make something like a wysiwyg-editor, and I'm using JS reg exp to preview the stuff that you type in in a div.

    I've managed to fix the easy parts, links a bold text, but I can't seem to get unordered lists to work. I want the syntax to be:

    [un-list]
    * First item
    [/un-list]

    And it's working, as long as I only have one list item. Can anyone help me with som regexp for this? And I'd really like to learn regexp, so explainations and links to further reading is appreciated!

  2. #2
    SitePoint Evangelist
    Join Date
    Mar 2006
    Location
    Sweden
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've gotten as far as this:
    text = text.replace( new RegExp( /\[un-list\]([^\[]+)\[\/un-list\]/gi ), '<ul>$1</ul>' );
    text = text.replace( new RegExp( /\*([^\*]+)/g ), '<li>$1</li>' );

    And it works, but if there is a * outside of an ul, it puts <li> around it, which of course it shouldn't. I've tried to only capture * inside <ul>, but can't make it work. Does anyone know how?

    And if my code is bad or ugly, please correct me.

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Regular expressions are stateless. They can't be used alone on something with a context. You can use the regex to tokenize the input, then run it through a state machine, to do the processing.

    If you don't mind using a fairly standardized syntax, rather than inventing your own, I'll suggest showdown, which is a javascript implementation of markdown.

  4. #4
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I gave up on bbcode, I look at it like flying to a world famous cheese store in the United States to get some Swiss cheese, when living in Switzerland.

    I think time would be much better spent cleaning HTML input up than it would be trying to re-invent the wheel with bbcode.

  5. #5
    SitePoint Evangelist
    Join Date
    Mar 2006
    Location
    Sweden
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I feel like giving up to, but I suppose this is a good opportunity to really learn regexp.

    So how do I use a grouped negated character class? This: x[^y] matches "x if it has anything but a y after it". But how do I do:
    \*([^\*|<\/ul>]+)? I want to match everything after a *, except another *, or a </ul>. I need to group <\/ul>, but I don't know how? I tried: \*([^\*|(<\/ul>)]+) but with no luck.

  6. #6
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sounds like you want a "negative lookahead".
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •