SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Regex question

  1. #1
    Who is Mr Blonde? Mr Blonde's Avatar
    Join Date
    Sep 2006
    Location
    Gold Coast, QLD
    Posts
    359
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regex question

    Hi i wasn't sure if this was the right place to put this question so please move if there is somewhere more appropriate.

    I'm pretty new to regular expressions, and have written one for a dreamweaver find an replace to strip an a tag if it links out of the current sites folder...

    find:
    Code:
    <a\s*href="../(.*)>(.*)</a>
    replace:
    Code:
    $2
    The Problem i have run into is that if there is an image inside the tag it will be deleted, or if there are two links in the one paragraph the content in between will be deleted too. It runs perfectly if the link is inside a list item for example.

    I think it is because if it runs into a second '>', it assumes that is the end of the 1st tag so deletes everything from there... is there a way to make it stop at the 1st '>'? (hope that made sense... it did in my head )

    thanks
    Heavens Rejects : Online Clothing Store
    Alternative Clothing : Mr Blonde
    Front end development : By the Webfella

  2. #2
    code ninja hamstu's Avatar
    Join Date
    Nov 2002
    Location
    Montréal, Canada
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, there is, you need to use the '?' (non-greedy) modifier.

    Try this:
    Code:
    <a\s*href="../(.*?)>(.*?)</a>

  3. #3
    Who is Mr Blonde? Mr Blonde's Avatar
    Join Date
    Sep 2006
    Location
    Gold Coast, QLD
    Posts
    359
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Awesome thanks heaps man, works a treat
    Heavens Rejects : Online Clothing Store
    Alternative Clothing : Mr Blonde
    Front end development : By the Webfella

  4. #4
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if all you want is $2 you should do it like this:

    <a\s*href="\.\./.*?>(.*?)</a>

    then replace with $1

    note the dots in the regexp should be escaped if you mean to match literal dots, otherwise each dot will match a single character of anything.

  5. #5
    Who is Mr Blonde? Mr Blonde's Avatar
    Join Date
    Sep 2006
    Location
    Gold Coast, QLD
    Posts
    359
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cool thanks for the tips kevin, good to know
    Heavens Rejects : Online Clothing Store
    Alternative Clothing : Mr Blonde
    Front end development : By the Webfella


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
  •