SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Guru
    Join Date
    Jan 2007
    Posts
    967
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Converting text in parenthesis to supertext regular expressions

    I would like to search text in a div and put numbers in parenthesis in supertext.

    So (1) would change to <sup>(1)</sup> but (xyz) would be left untouched.
    I would also like to include commas in the supertext.
    So (5),(8) would be converted to <sup>(5),(8)</sup>

    Does anyone know how to do this?

    Thank You E

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    You can do it like this:

    Code:
    var p = document.querySelectorAll("p"),
        myRegex = /(\((\d+)\))/g;
    
    for(var i=0; i < p.length; i++){
        p[i].innerHTML = p[i].innerHTML.replace(myRegex, function(match) { return "<sup>" + match + "</sup>"; });
    }
    fiddle

  3. #3
    SitePoint Guru
    Join Date
    Jan 2007
    Posts
    967
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you! Is there any way to include a comma in between the parenthesis. IE: (1),(2)?

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    So you want:

    Code:
    (1) foo bar (2) foo bar (3) foo bar
    to become:

    Code:
    <sup>(1)</sup> foo bar, <sup>(2)</sup> foo bar, <sup>(3)</sup> foo bar
    Is that correct?

  5. #5
    SitePoint Guru
    Join Date
    Jan 2007
    Posts
    967
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    That would work, but actually only a comma would be safer. It would be less likely to trigger false responses.

    Thank you!!!

  6. #6
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    Quote Originally Posted by eruna View Post
    That would work, but actually only a comma would be safer. It would be less likely to trigger false responses.
    Then I have misunderstood what you are aiming for.
    Could you give an example of normal text, then of what you would like it to become (as I do above).

  7. #7
    SitePoint Guru
    Join Date
    Jan 2007
    Posts
    967
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    This is one I'm looking for:

    Code:
     Foo Bar (1) ,(2),(3) Foo Bar (1),(2)
    to
    Code:
     Foo Bar <sup>(1) ,(2),(3)</sup> Foo Bar <sup>(1),(2)</sup>
    Thank you!!!!

    Michael

  8. #8
    SitePoint Addict bronze trophy mawburn's Avatar
    Join Date
    Apr 2014
    Posts
    203
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by eruna View Post
    This is one I'm looking for:

    Code:
     Foo Bar (1) ,(2),(3) Foo Bar (1),(2)
    to
    Code:
     Foo Bar <sup>(1) ,(2),(3)</sup> Foo Bar <sup>(1),(2)</sup>
    Thank you!!!!

    Michael
    Getting complicated.

    This should work:

    Code:
    (\((\d+)\))((\s*,\s*)?\((\d+)\))*
    http://regexpal.com/?flags=g&regex=(...%20(1)%2C%20(2)

    Looks messy though, it could probably be solved more elegantly.

    Code javascript:
    myRegex = /(\((\d+)\))((\s*,\s*)?\((\d+)\))*/g;

  9. #9
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    I would do it like this:

    Code:
    <p>Foo Bar (1) ,(2),(3) Foo Bar (1),(2)</p>
    
    var p = document.querySelectorAll("p"),
        myRegexp = /\([()0-9 ,]\)/g;
    
    for(var i=0; i < p.length; i++){
        p[i].innerHTML = p[i].innerHTML.replace(myRegexp, function(match) { return "<sup>" + match + "</sup>"; });
    }
    fiddle

  10. #10
    SitePoint Addict bronze trophy mawburn's Avatar
    Join Date
    Apr 2014
    Posts
    203
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    I would do it like this:

    Code:
        myRegexp = /\([()0-9 ,]\)/g;
    That does each (#) set individually and misses the comma.

    regexpal

  11. #11
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by mawburn View Post
    That does each (#) set individually and misses the comma.
    Oh yeah, well spotted.
    The regex should be:

    Code:
    myRegexp = /\([()0-9 ,]\+)/g;
    I forgot the plus to make it greedy.

    fiddle

  12. #12
    SitePoint Guru
    Join Date
    Jan 2007
    Posts
    967
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys!!!

    One of these days I will master these regular expressions too.

    E

  13. #13
    SitePoint Guru
    Join Date
    Jan 2007
    Posts
    967
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    It worked! I just tried it out.


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
  •