/[HELP!]/ regxp replace uppercase/lowercase

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • brendan

    #1

    /[HELP!]/ regxp replace uppercase/lowercase

    Hi,
    I need to get replace all the uppercase characters in a string with a
    lowercase character and a '-'
    __ie _______________ _______________ _______________ _____
    'backgroundColo r' becomes 'background-color'
    'borderWidth' becomes 'border-width';
    -----------------------------------------------------


    in PHP strtolower(ereg _replace("[A-Z]", "-\\0" , $var)); works
    but obviously I'm not using php
    any help much appreciated
    cheers
    brendan.

  • Lasse Reichstein Nielsen

    #2
    Re: /[HELP!]/ regxp replace uppercase/lowercase

    brendan <brendan@justic e.com> writes:
    [color=blue]
    > I need to get replace all the uppercase characters in a string with a
    > lowercase character and a '-'[/color]
    [color=blue]
    > in PHP strtolower(ereg _replace("[A-Z]", "-\\0" , $var)); works
    > but obviously I'm not using php
    > any help much appreciated[/color]

    javascript regular expressions are closer to the ones in Perl.
    The closest Javascript to your PHP example would be:

    var newString = oldString.repla ce(/[A-Z]/g,"-$&").toLowerCas e();

    A more exotic method (just for the fun of it) is:

    var newString = oldString.repla ce(/[A-Z]/g,function(s){
    return "-"+s.toLowerCase ();
    });

    Both work in Netscape 4, IE6, Opera 7, and Mozilla FB 0.6.
    The latter doesn't work in Netscape 3, Opera 6 or IE 5 (or earlier).

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • brendan

      #3
      Re: /[HELP!]/ regxp replace uppercase/lowercase

      Lasse Reichstein Nielsen wrote:[color=blue]
      > brendan <brendan@justic e.com> writes:
      >
      >[color=green]
      >>I need to get replace all the uppercase characters in a string with a
      >>lowercase character and a '-'[/color]
      >
      >[color=green]
      >>in PHP strtolower(ereg _replace("[A-Z]", "-\\0" , $var)); works
      >>but obviously I'm not using php
      >>any help much appreciated[/color]
      >
      >
      > javascript regular expressions are closer to the ones in Perl.
      > The closest Javascript to your PHP example would be:
      >
      > var newString = oldString.repla ce(/[A-Z]/g,"-$&").toLowerCas e();
      >
      > A more exotic method (just for the fun of it) is:
      >
      > var newString = oldString.repla ce(/[A-Z]/g,function(s){
      > return "-"+s.toLowerCase ();
      > });
      >
      > Both work in Netscape 4, IE6, Opera 7, and Mozilla FB 0.6.
      > The latter doesn't work in Netscape 3, Opera 6 or IE 5 (or earlier).
      >
      > /L[/color]

      Brilliant!
      This workds perfectly thankyou so much.
      b.

      Comment

      Working...