...

View Full Version : Javascript Regular Expressions Replace question?



Orange Gold
01-03-2011, 10:04 PM
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:

5
how can I detect the number "2" and put it superscripted past the number 5.


so the javascript reads this:
5

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

thanks!

siberia-man
01-03-2011, 10:59 PM
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.:



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::



var s = '5';
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.

Krupski
01-03-2011, 11:05 PM
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.:



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::



var s = '5';
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!

Krupski
01-03-2011, 11:16 PM
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

siberia-man
01-03-2011, 11:32 PM
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 x. Of course we can modify this regexp to cover all cases of any powers and bases. For example:

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

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

[power=2]2

Why did I think about this? You know -- users are so unpredictable :)

Old Pedant
01-04-2011, 12:19 AM
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.

siberia-man
01-04-2011, 12:39 AM
... you should not limit ...

totally agree with you and my final answer is /\[power=(.+?)\](.+?)\[\/power\]/

Krupski
01-04-2011, 04:22 AM
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 x. Of course we can modify this regexp to cover all cases of any powers and bases. For example:

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

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

[power=2]2

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:

216 (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! :thumbsup:

Orange Gold
01-04-2011, 09:56 PM
Okay.. so I have tried applying this to a square root function..
If you type something like 27 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:


paramString = paramString.split(/\/g).join("<sup>$1</sup>√<font style='text-decoration: overline;'>");
paramString = paramString.split("").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

Orange Gold
01-05-2011, 02:06 AM
Bump. :thumbsup:

rnd me
01-05-2011, 06:24 AM
convert your .split()s into .replace()s

Orange Gold
01-05-2011, 06:37 AM
Works great! thanks!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum