SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regex: match less than / more than symbols but not tags

    Hi guys,

    I'm a bit stumped on a regular expression problem.

    I want to match a "less than" symbol but not tag brackets in a string.

    I'm assuming what I want to look for is a < which isn't followed by a > in the rest of the string.

    Any ideas how to write that as a regex?

    Many thanks!

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could remove all the pairs:

    /<[^>]*>/

    and replace them with "", and then search for the singletons.

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot for that. Is there a way I could put the tags back in afterwards?

    I'm basically validating a string to make sure all less than /more than symbols are HTML formatted, and want to leave the tags intact.

    Nice one

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think this works ...

    /<(?=[^>]*<)/g

    "<b> < lorem < </b>" to "<b> &lt; lorem &lt; </b>"

    Man Regex is a double hard b*stard!

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot for that. Is there a way I could put the tags back in afterwards?
    Uhmm...do the replace on a copy of the original?


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
  •