Results 1 to 7 of 7

Thread: Getting Regular Expression To Repeat

  1. #1
    Join Date
    Aug 2006
    Location
    North Las Vegas, Nevada
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Getting Regular Expression To Repeat

    I'm trying to write a regex that replaces all spaces with hyphens, but only if those spaces are within an h2. I want it to repeat as many times as I need to through a document, but it only ever hits the first.

    Code:
    (^[<h2>]+)?((\w*)*)((\s*)*)
    Replacing with

    Code:
    $1$2-
    This is as close as I've been able to get it to produce so far

    HTML Code:
    <h2>dota dota dota dota dota</h2>
    To

    HTML Code:
    <h2>dota-dota-dota-dota-dota-</h2>
    I'm not even 100% sure why it puts that hyphen just before the last h2, but I'm much more concerned about it repeating.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,957
    Thanks
    43
    Thanked 3,195 Times in 3,157 Posts
    Blog Entries
    12

    Default

    What language are you using? Javascript, PHP, something else?

    And can you show the whole script?

    How about:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    </head>
    <body>
    <h2>dota dota dota dota dota</h2>
    <h2>bob bob bob bob bob</h2>
    <script type="text/javascript">
    var h2s = document.getElementsByTagName('h2'), h2 = h2s.length;
    while(--h2 > -1){
    	h2s[h2].innerHTML = h2s[h2].innerHTML.replace(/\s/gm, '-');
    }
    </script>
    </body>
    </html>
    Last edited by jscheuer1; 06-21-2014 at 11:54 PM. Reason: add code snippet
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,712
    Thanks
    82
    Thanked 90 Times in 88 Posts

    Default

    Just for fun here's an example using php:

    Code:
    <?php
    $string = " <h2>dota dota dota dota dota  </h2> ";
    $string = preg_replace('/ (?=((?!<\/?h2\b|>).)*<\/h2)/','-',$string);
    echo "$string";
    ?>
    To choose the lesser of two evils is still to choose evil. My personal site

  4. #4
    Join Date
    Aug 2006
    Location
    North Las Vegas, Nevada
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Sorry about that, I should have mentioned that I've been using Dreamweavers find and replace with "Use Regular Expression" turned on.

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,957
    Thanks
    43
    Thanked 3,195 Times in 3,157 Posts
    Blog Entries
    12

    Default

    Sorry, I'm not aware of how DW utilizes RE. I can tell you that using ^ (if it means in DW what it means in all RE's) is not the best choice, because it means the beginning of a line. If the h2's you are targeting are not always at the beginning of a line, they will be skipped. You should be able to simply skip using ^ to correct that. Now, is there a switch in DW to force global over first instance? If so that should be applied. But simply dropping the ^ might be all you need to solve that problem (global is the default in my word processor that takes RE as an option).
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Aug 2006
    Location
    North Las Vegas, Nevada
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Maybe for Dreamweaver, the answer is that there isn't one for this specific problem. I tried taking ^ out and it spit this out:

    HTML Code:
    <h2>asldk-adksfj-asdlkfj-asdkfj-kdsaf-kjk-<-/h2>
    Even less sure why it put a hyphen just before the slash. Unfortunately, the only checkbox available concerning regular expressions is "use regular expression".

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,957
    Thanks
    43
    Thanked 3,195 Times in 3,157 Posts
    Blog Entries
    12

    Default

    Read the online help then. I meant switches like g (global) or m (multiline). In javascript and most other languages you put a comma at the end of the expression and follow that with one or more switches:

    (^[<h2>]+)?((\w*)*)((\s*)*),gm
    or:

    /(^[<h2>]+)?((\w*)*)((\s*)*)/,gm
    That may or may not work with DW. I'm sure it has documentation somewhere:

    http://lmgtfy.com/?q=dreamweaver+reg...pression+usage
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. Regular expression errors
    By techno_race in forum PHP
    Replies: 2
    Last Post: 02-25-2010, 05:34 PM
  2. regular expression problem
    By rogermsw in forum PHP
    Replies: 3
    Last Post: 12-06-2009, 01:14 AM
  3. Regular Expression Help
    By Moshambi in forum JavaScript
    Replies: 1
    Last Post: 04-17-2009, 07:29 AM
  4. Regular Expression Help CSV
    By JasonDFR in forum PHP
    Replies: 4
    Last Post: 10-26-2008, 06:49 PM
  5. Regular Expression tutorial??
    By shachi in forum Other
    Replies: 14
    Last Post: 10-06-2006, 02:05 PM

Tags for this Thread

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
  •