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 12 of 12
  1. #1
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript Regular Expressions Replace question?

    Hello!

    Formerly I've been replacing characters with javascript using the following code.
    paramString = paramString.split("").join("<sup>");
    paramString = paramString.split("
    ").join("</sup>");

    So if the user on my website types 52 it would return the following value:
    5<sup>2</sup>, or just, 5

    So...

    my question is.. how do I detect something like this.
    if they enter:

    [power=2]5[/power]
    how can I detect the number "2" and put it superscripted past the number 5.


    so the javascript reads this:
    [power=2]5[/power]

    and returns this:
    5<sup>2</sup>

    thanks!
    Last edited by Orange Gold; 01-03-2011 at 10:17 PM.

  • #2
    Regular Coder
    Join Date
    Sep 2010
    Location
    Far far away
    Posts
    122
    Thanks
    0
    Thanked 16 Times in 16 Posts
    Hi Orange Gold,

    First of all. You can use the code as you shown above but I recommend you not use it. The better way is to use the 'replace' method. At least the single regexp covers the job made of two couples of 'split/join' methods.:

    Code:
    var s = '52';
    var r = s.replace(/\[(\/?sup)\]/g, '<$1>');
    
    alert(s);
    alert(r);
    Next one. To detect some substrings from the whole string use 'match/replace' methods. In your case the 'replace' method will be proper::

    Code:
    var s = '[power=2]5[/power]';
    var r = s.replace(/\[power=(\d+)\](.+?)\[\/power\]/g, '$2<sup>$1</sup>');
    
    alert(s);
    alert(r);
    In the future I extremely recommend you to learn regular expressions by the Jeff Friedl's brilliant book.
    Last edited by siberia-man; 01-03-2011 at 11:09 PM.

  • #3
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by siberia-man View Post
    Hi Orange Gold,

    First of all. You can use the code as you shown above but I recommend you not use it. The better way is to use the 'replace' method. At least the single regexp covers the job made of two couples of 'split/join' methods.:

    Code:
    var s = '52';
    var r = s.replace(/\[(\/?sup)\]/g, '<$1>');
    
    alert(s);
    alert(r);
    Next one. To detect some substrings from the entire string use Next one. To detect some substrings from the whole string use 'match/replace' methods. In your case the 'replace' method will be proper::

    Code:
    var s = '[power=2]5[/power]';
    var r = s.replace(/\[power=(\d+)\](.+?)\[\/power\]/g, '$2<sup>$1</sup>');
    
    alert(s);
    alert(r);
    In the future I extremely recommend you to learn regular expressions by the Jeff Friedl's brilliant book.
    Quick! I just came up with this:

    str = str.replace(/\[power=([0-9])\]([0-9])\[\/power\]/g, '$2<sup>$1</sup>');

    LOL!
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #4
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by siberia-man View Post
    Code:
    var r = s.replace(/\[power=(\d+)\](.+?)\[\/power\]/g, '$2<sup>$1</sup>');
    Question: Why did you use (.+?) for the second number instead of using (\d+)?

    -- Roger
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #5
    Regular Coder
    Join Date
    Sep 2010
    Location
    Far far away
    Posts
    122
    Thanks
    0
    Thanked 16 Times in 16 Posts
    Quote Originally Posted by Krupski View Post
    Question: Why did you use (.+?) for the second number instead of using (\d+)?
    There is simple reason and you can answer for your question by youself. Your example covers all cases of one digit numbers only (both power and base). But my example covers all cases of numerical powers and non-numerical bases like [power=2]x[/power]. Of course we can modify this regexp to cover all cases of any powers and bases. For example:

    /\[power=(.+?)\](.+?)\[\/power\]/

    But I'd like to notice all my examples are not able to process the following code:

    [power=2][power=2]2[/power][/power]

    Why did I think about this? You know -- users are so unpredictable
    Last edited by siberia-man; 01-03-2011 at 11:34 PM.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Ummm...but a common way to write "the square root of 3" is 3<sup>0.5</sup>

    And, of course the reciprocal of 3-squared can be written 3<sup>-2</sup>

    And there's really no reason you should avoid x<sup>y</sup>

    So, really, you should not limit the power to just an integer number, either.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Regular Coder
    Join Date
    Sep 2010
    Location
    Far far away
    Posts
    122
    Thanks
    0
    Thanked 16 Times in 16 Posts
    Quote Originally Posted by Old Pedant View Post
    ... you should not limit ...
    totally agree with you and my final answer is /\[power=(.+?)\](.+?)\[\/power\]/

  • #8
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by siberia-man View Post
    There is simple reason and you can answer for your question by youself. Your example covers all cases of one digit numbers only (both power and base). But my example covers all cases of numerical powers and non-numerical bases like [power=2]x[/power]. Of course we can modify this regexp to cover all cases of any powers and bases. For example:

    /\[power=(.+?)\](.+?)\[\/power\]/

    But I'd like to notice all my examples are not able to process the following code:

    [power=2][power=2]2[/power][/power]

    Why did I think about this? You know -- users are so unpredictable
    Very good. Actually, when I write "real" programs, I'm very careful to try and catch any and all possible variations and screw-ups that the user is likely to make (i.e. I try to make the code "idiot proof").

    The regex post, however, I threw together in a hurry. Yours is definitely more robust.

    As another variation, if I were using it, I may do something like this:

    [power=(1/3)]216[/power] (cube root of 216). My regex wouldn't handle that! Yours returns '216<sup>(1/3)</sup>'..

    It parsed correctly, but I doubt the OP's BBCode would handle it (or would it?).

    Anyway, thumbs up to you!
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #9
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay.. so I have tried applying this to a square root function..
    If you type something like [rt=3]27[/rt] then I want it to show somewhat 3√27
    Unfortunately, this is what was displayed?
    $1√3$1√27[/rt]

    Here is my replacement code:
    Code:
    paramString = paramString.split(/\[rt=(.+?)\]/g).join("<sup>$1</sup>√<font style='text-decoration: overline;'>");
    paramString = paramString.split("[/rt]").join("</font>");

    Edit: also... this quote is way to complex, ""Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov" hehehehehe

    Thanks for all the help so far guys! I'm an actionscript type of guy so javascript is news to me :P
    Last edited by Orange Gold; 01-04-2011 at 10:03 PM.

  • #10
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Bump.

  • #11
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    convert your .split()s into .replace()s
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #12
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Works great! thanks!


  •  

    Posting Permissions

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