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 8 of 8
  1. #1
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts

    looking for a regular expression

    All the expression I meet are nutters ...

    No anyway - I'm hoping someone with a good grasp of regexp can help me with this. Basically, I want to be to replace predictable pairs of HTML tags with different tags, without affecting what's between them. So for example

    <span class="small"> ... anything ... </span>

    could become

    <small> ... anything ... </small>

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Flint, Michigan, USA
    Posts
    617
    Thanks
    1
    Thanked 20 Times in 20 Posts
    str='before<span class="small">during</span>after';
    newstr = str.replace(/^(.*)<span class="small">(.*)<\/span>(.*)$/, "$1<small>$2<\/small>$3");
    alert(newstr);

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks; that's the business, but I was hoping for a general regexp rather than a javascript expression - something I can use in a search/replace text editor. Co0uld you help modifying it?

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Flint, Michigan, USA
    Posts
    617
    Thanks
    1
    Thanked 20 Times in 20 Posts
    I don't understand. What would you like that would be different?

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    183
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Then I think just /^(.*)<span class="small">(.*)<\/span>(.*)/ would work in finding a match...

    I didn't think it was possible to replace stuff around any data without losing that data... I've always just replaced one side and then the other.
    Offtone.com - In the works...

  • #6
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72 W. 48' 57" , 41 N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    i don't know how familiar you are with RE's, so here's a complete breakdown of it.

    /^(.*)<span class="small">(.*)<\/span>(.*)$/$1<small>$2<\/small>$3/

    ^ is the start of the string, and (.*) means 0 or more of any character. so it's string start, everything but the HTML, everything inside the HTML, the end of the HTML, then everything else, then the end of the string. in this case, the start of the string would be the start of the file (or the start of the line, but i think in a text editor it would be the start of the file) and the end of the string would be the end of the file.

    then, the second part of the RE is what should be used to replace the matching text. in a RE, when you enclose things in parenthesis, they get grouped together, and assigned a number, so you can reference that group later. hence the $1, $2, and $3. that all translates to everything that was before the old html, the new html, everything that was between the old html, the new html, everything that was after the old html.

    it seems to me like you're looking to replace bunches of occurrences of this string throughout a file. i don't know if that's real;y what you want, and i don't know if Jalarie's would do that. i'm far from an expert with RE's, but i think that Jalarie's would match one occurrence within the file. as i understand it, to match multiple occurrences, you'd need the following:

    /<span class="small">(.*)<\/span>/<small>$1<\/small>/gi

    someone please let me know if that's wrong.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah, I was looking for something I can use in a search and replace editor to change all occurences of that tag in a whole site, and I'm also not at familiar with reg expressions.

    so thanks all - very helpful

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you try to use the RegExp above in a global search-and-replace action, beware of the greediness of the quantifiers. They might match more than you wanted to.

    And don't forget to make a backup of your original file in case anything goes wrong (an advice from someone who didn't do it during his first exposure to RegExp and hated himself for not thinking about that beforehand).


  •  

    Posting Permissions

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