How to replace ^ with an other character in a string ?

Discussion in 'Javascript' started by ast, Mar 10, 2011.

  1. ast

    ast Guest

    hi

    I am trying to replace the character ^ in a string with character %
    using "replace" string method. I know that ^ is a special character
    so it has to be backslashed. But it doesnt work.

    example

    <html>
    <body>
    <script type="text/javascript">

    var s = "x^12+2";
    var s2 = "";

    var reg = new RegExp("\^", "g");

    s2 = s.replace(reg, "%" );

    document.write(s2);

    </script>
    </body>
    </html>

    It provide %x^12+2 instead of x%12+2
    I tried many variations

    var reg = new RegExp("^", "g");
    var reg = new RegExp("/^", "g");
    var reg = new RegExp('^', "g");

    but nothing works.
    Same behavior with IE8 and firefox 3.6.8

    Any idea ?
     
    ast, Mar 10, 2011
    #1
    1. Advertising

  2. ast

    ast Guest

    "China Blue Jay Way" <> a écrit dans le message de
    news:chine.bleu-AA75FE.08224410032011@reserved-multicast-

    >>
    >> var s = "x^12+2";
    >> var s2 = "";

    >
    > var p = s.indexOf('^');
    > if (p>=0) s2 = s.substr(0,p-1)+'%'+s.substr(p+1);
    > else s2 = s;
    >


    yes, but what i really want to do is more complex. So i need
    to use the "replace" method.
     
    ast, Mar 10, 2011
    #2
    1. Advertising

  3. ast

    ast Guest

    "Jake Jarvis" <> a écrit dans le message de
    news:-berlin.de...


    >
    > It should have been
    >
    > new RegExp("\\^", "g");
    >
    > \ has special meaning in string literals already, in order
    > to have it in the 'end string' you must escape it.
    >


    yes it works thanks. but I dont understand why.

    for example, /a*/ means match 0 or more a's. To match * literally,
    precede it with a backslash; for example, /a\*/ matches 'a*'.

    why do i need 2 backslash to espace ^ and only one to escape * ?
     
    ast, Mar 10, 2011
    #3
  4. ast

    ast Guest

    "ast" <> a écrit dans le message de news:4d7915cc$0$18109$...
    >
    > "Jake Jarvis" <> a écrit dans le message de
    > news:-berlin.de...
    >
    >
    >>
    >> It should have been
    >>
    >> new RegExp("\\^", "g");
    >>
    >> \ has special meaning in string literals already, in order
    >> to have it in the 'end string' you must escape it.
    >>

    >
    > yes it works thanks. but I dont understand why.
    >
    > for example, /a*/ means match 0 or more a's. To match * literally,
    > precede it with a backslash; for example, /a\*/ matches 'a*'.
    >
    > why do i need 2 backslash to espace ^ and only one to escape * ?
    >



    I found answer:

    Also do not forget to escape \ itself while using the new RegExp("pattern")
    notation since \ is also an escape character in strings.

    so we have to write:

    reg = /\^/;
    or
    reg = new RegExp("\\^", "g");
     
    ast, Mar 10, 2011
    #4
  5. ast wrote:

    > | Also do not forget to escape \ itself while using the new
    > | RegExp("pattern") notation since \ is also an escape character
    > | in strings.
    >
    > so we have to write:
    >
    > reg = /\^/;
    > or
    > reg = new RegExp("\\^", "g");


    No. Either

    reg = /\^/g;

    or

    reg = new RegExp("\\^", "g");

    Or

    reg = /\^/;

    or

    reg = new RegExp("\\^");


    PointedEars
    --
    var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
    ) // Plone, register_function.js:16
     
    Thomas 'PointedEars' Lahn, Mar 10, 2011
    #5
  6. ast

    Ry Nohryb Guest

    On Mar 10, 5:05 pm, "ast" <> wrote:
    > hi
    >
    > I am trying to replace the character ^ in a string with character %
    > using "replace" string method. I know that ^ is a special character
    > so it has to be backslashed. But it doesnt work. (...)


    "x^12+2^2".replace(/\^/g, '%');
    -> "x%12+2%2"
    --
    Jorge.
     
    Ry Nohryb, Mar 11, 2011
    #6
  7. ast

    Ry Nohryb Guest

    On Mar 11, 1:44 am, Ry Nohryb <> wrote:
    > On Mar 10, 5:05 pm, "ast" <> wrote:
    >
    > > hi

    >
    > > I am trying to replace the character ^ in a string with character %
    > > using "replace" string method. I know that ^ is a special character
    > > so it has to be backslashed. But it doesnt work. (...)

    >
    > "x^12+2^2".replace(/\^/g, '%');
    > -> "x%12+2%2"


    Also, for more complicated transformations you can pass a function as
    the 2nd argument to .replace():

    var ordinal= 0;
    function Æ’ (char, pos) {
    ordinal++;
    var txt= "[ordinal:"+ ordinal;
    txt+= ", pos:"+pos;
    txt+= ", char:"+ char
    txt+= "]";
    return txt;
    }

    "x^12+2^2".replace( /\^/g, Æ’ )
    -> "x[ordinal:1, pos:1, char:^]12+2[ordinal:2, pos:6, char:^]2"
    --
    Jorge.
     
    Ry Nohryb, Mar 11, 2011
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brand Bogard

    8 bit character string to 16 bit character string

    Brand Bogard, May 25, 2006, in forum: C Programming
    Replies:
    8
    Views:
    763
    those who know me have no need of my name
    May 28, 2006
  2. Umesh
    Replies:
    17
    Views:
    890
  3. herman
    Replies:
    5
    Views:
    7,654
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Aug 30, 2007
  4. Victor
    Replies:
    3
    Views:
    143
    Victor
    Sep 12, 2003
  5. Bart Vandewoestyne
    Replies:
    8
    Views:
    789
    Bart Vandewoestyne
    Sep 25, 2012
Loading...

Share This Page