regExp problem

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

    #1

    regExp problem

    Hi, all
    I tried this and it works.
    ------
    var s1=" xxxx ";
    alert(">>"+s1.r eplace(/^\s+/, " ")+"<<");


    But why this won't work
    ------
    var s1=" xxxx";
    var reg=new RegExp("^\s+");
    alert(">>"+s1.r eplace(reg," ")+"<<");



    jack


  • Lee

    #2
    Re: regExp problem

    datactrl said:
    [color=blue]
    >But why this won't work[/color]
    [color=blue]
    > var reg=new RegExp("^\s+");[/color]

    The value of the string being passed to the RegExp constructor
    is "^s+". The backslash is consumed as the literal string is
    parsed. If you want the string to contain a backslash, you
    have to escape it by preceding it with another backslash:

    new RegExp("^\\s+") ;

    or simply use a RegExp literal:

    var reg=/^\s+/;

    Comment

    • datactrl

      #3
      Re: regExp problem

      Thanks Lee. It works.

      Jack
      "Lee" <REM0VElbspamtr ap@cox.net> wrote in message
      news:c8g77r01di b@drn.newsguy.c om...[color=blue]
      > datactrl said:
      >[color=green]
      > >But why this won't work[/color]
      >[color=green]
      > > var reg=new RegExp("^\s+");[/color]
      >
      > The value of the string being passed to the RegExp constructor
      > is "^s+". The backslash is consumed as the literal string is
      > parsed. If you want the string to contain a backslash, you
      > have to escape it by preceding it with another backslash:
      >
      > new RegExp("^\\s+") ;
      >
      > or simply use a RegExp literal:
      >
      > var reg=/^\s+/;
      >[/color]


      Comment

      Working...