[solved] find within a string

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

[solved] find within a string

Post by phpScott »

I suck at writing regual expressions so if some one call help that would great.

String looks like.

background: rgb(156, 123, 173)
url(/images/interface/side_bar_02.gif) repeat-x scroll 0%; font-family:
verdana,arial,sans-serif; color: rgb(156, 123, 173); line-height:
1.5em; -moz-background-clip: initial; -moz-background-origin: initial;
-moz-background-inline-policy: initial; font-size: 0.8em;

I need to find font-size: 0.8em;

so I would like to do a search for 'font-size:' and the 'em;' part regardless what the number in the middle is set too.

this is client side as it is manipulting the css stylesheet.
Cheers

phpScott
Last edited by phpScott on Thu May 05, 2005 7:55 am, edited 1 time in total.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

you could do something like this :

Code: Select all

$str = explode(',','
background: rgb(156, 123, 173) 
url(/images/interface/side_bar_02.gif) repeat-x scroll 0%; font-family: 
verdana,arial,sans-serif; color: rgb(156, 123, 173); line-height: 
1.5em; -moz-background-clip: initial; -moz-background-origin: initial; 
-moz-background-inline-policy: initial; font-size: 0.8em;');

for($i=0; $i<count($str); $i++)
{
   if($i==7)
   {
      echo $str[$i];
   }
}
of course this assumes that your font will be the 7th semi-colon delimited entry in the string every time. you coudl also do a array search to find the font-size pattern, but this should be enough to get you started ;)

hope this helps...
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

solved while waiting for replies?

Code: Select all

find = &quote;font-size: &quote; + currentSize + &quote;em;&quote;;
newSize = &quote;font-size: &quote; + size + &quote;em;&quote;;
testing = bodyRule.replace(find, newSize);
I know that replace should be using a regular expression but it seems to work.

Cheers though.

phpScott
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

phpScott wrote: I know that replace should be using a regular expression but it seems to work.
The replace() method CAN take a regular expression as an argument but it will work with a string since /hello world/ is still a regular expression, just no meta characters in there ;-)
Post Reply