Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    May 2006
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Interesting RegExp challenge

    Hi... I want to use a regexp to detect if there are any HTML tags in a body of text that are NOT one of the following:

    p
    h1
    h2
    h3
    h4
    h5
    h6
    em
    sub
    sup
    a

    I want to know if any other tags are used so I can throw an error...
    The use of negatives is what makes this regex hard...
    Any advice is appreciated!

  • #2
    Regular Coder BonRouge's Avatar
    Join Date
    Mar 2006
    Location
    Sendai, Japan
    Posts
    328
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How about this?
    Code:
    '/<(?!p|h1|h2|h3|h4|h5|h6|em|sub|sup|a|\/)(|.+)>/Ui'
    Last edited by BonRouge; 02-22-2007 at 11:56 AM.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,985
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    This seems to work:

    if (/<(?!p|h1|h2|h3|h4|h5|h6|em|sub|sup|a)/gi.test(lstr.value)) {

  • #4
    Regular Coder
    Join Date
    May 2006
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, I found a way...

    It's a slightly modified version of the one above. The only problem with the one above is that it gets triggered by the closing tags...

    "\/?" should catch the closing tags, but the problem is (and I'm still not sure I understand why) it gets triggered by the closing tags as well. I've stopped this by having it look ahead to make sure the next character isn't the closing / either.

    <\/?(?!p|h1|h2|h3|h4|h5|h6|em|sub|sup|a|\/).+>


  •  

    Posting Permissions

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