lower case conversion in replace function

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

    lower case conversion in replace function

    Hi,

    I'trying to use a regExp in Javascript to replace any all upper case word in a string by the same word having only its first letter in upper case.
    This expression detects the words:
    /([A-ZÄÀÉÈÊËÎÏÔÙÛÜ])([A-ZÄÀÉÈÊËÎÏÔÙÛÜ]+)/g
    and backreferences $1 and $2 will contain the first letter and the rest of the word respectively.

    Now, how can I convert $2 to lower case in the replace function?
    I would need something like:
    replace(/([A-ZÄÀÉÈÊËÎÏÔÙÛÜ])([A-ZÄÀÉÈÊËÎÏÔÙÛÜ]+)/g, "$1" + "$2".toLowerCas e())
    But this does not work since the toLowerCase() is applied first to the string "$2", THEN the string $2 is replaced by the second match.

  • Janwillem Borleffs

    #2
    Re: lower case conversion in replace function

    Claude Schneegans wrote:[color=blue]
    > Now, how can I convert $2 to lower case in the replace function?
    > I would need something like:
    > replace(/([A-ZÄÀÉÈÊËÎÏÔÙÛÜ])([A-ZÄÀÉÈÊËÎÏÔÙÛÜ]+)/g, "$1" +
    > "$2".toLowerCas e())
    > But this does not work since the toLowerCase() is applied first to
    > the string "$2", THEN the string $2 is replaced by the second match.[/color]

    Try:

    var s = "ABCDE FGHI";
    s = s.replace(
    /([A-ZÄÀÉÈÊËÎÏÔÙÛÜ]+)/g,
    function (w) {
    return w.charAt(0).toU pperCase() +
    w.substring(1). toLowerCase();
    }
    );
    alert(s);


    HTH;
    JW



    Comment

    • @SM

      #3
      Re: lower case conversion in replace function



      Claude Schneegans a ecrit :[color=blue]
      >
      > Hi,
      >
      > I'trying to use a regExp in Javascript to replace any all upper case word in a string by the same word having only its first letter in upper case.
      > This expression detects the words:
      > /([A-ZÄÀÉÈÊËÎÏÔÙÛÜ])([A-ZÄÀÉÈÊËÎÏÔÙÛÜ]+)/g
      > and backreferences $1 and $2 will contain the first letter and the rest of the word respectively.[/color]

      truc=truc.charA t(0).toUpperCas e()+truc.substr ing(1,truc.leng th).toLowerCase ;
      [color=blue]
      >
      > Now, how can I convert $2 to lower case in the replace function?[/color]

      $something is not accepted in JS language !

      --
      ******** (enlever/remove [OTER_MOI] du/from reply url) *******
      Stéphane MORIAUX : mailto:stephane OTER_MOImoriaux @wanadoo.fr
      Aide aux Pages Perso (images & couleurs, formulaire, CHP, JS)

      *************** *************** *************** *************** **

      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: lower case conversion in replace function

        "@SM" <stephanemorOTE Z_MOIiaux@wanad oo.fr> writes:
        [color=blue]
        > $something is not accepted in JS language ![/color]

        Is so! :P

        Javascript identifiers may contains the letters (a-z, any case),
        digits (0-9), underscore (_) and dollar sign ($). The first character
        may not be a digit. Read ECMA 262, section 7.6.

        The $ is *intended* for mechanically generated code, but there is no
        requirements.

        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        Working...