Replace escape characters driving me mad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • metamind
    New Member
    • Oct 2007
    • 3

    Replace escape characters driving me mad

    I have a string called "s" which contains "problems once you\x19ve secured the job!)."

    I am trying to replace the \x19 character with "ww" in this case but

    s.replace(/\x19/g, 'ww')

    is just returning exactly the same thing. What am I doing wrong. This is driving me mad.

    Many thanks.

    Transcript below:

    $[3] = [string] "problems once you\x19ve secured the job!)."
    0002: s.replace(/\x19/g, 'ww')
    $[4] = [string] "problems once you\x19ve secured the job!)."
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    hi ...

    have you tried something like the following ... that should work:

    [CODE=javascript]var s = "problems once you\x19ve secured the job!"
    s = s.replace(/\x19/g, 'ww');
    alert(s);
    [/CODE]
    kind regards

    Comment

    • metamind
      New Member
      • Oct 2007
      • 3

      #3
      Hi,

      Thanks for that. The wierd thing is that the escaped character does not seem to be behaving properly. The string "s" is pasted into a textarea. The original looks like this"impression s counting – "you have".

      I am looking at the string in a console (venkman in firefox). When I get it to print s it looks like this:

      "impression s counting \x13 \"you have"

      if I do:

      s.indexOf('c')

      I get:

      [integer] 12

      Which is what you would expect. But when I do:

      s.indexOf('\x13 ')

      I get:

      [integer] -1

      Suggesting that '\x13' is not in the string at all.

      I have done all sorts but I still can't seem to locate the escaped character in the string. This is wierd since if I set up a string var v = "impression s counting \x13 \"you have" then I can see the character fine. I have included a transcript of what I have been doing at the end of this post after the ===== . (0001: is my prompt, lines starting $ are the returned values.

      Wierd indeed!

      Any ideas welcome

      Regards


      =============== =============== ========
      0001: s
      $[1] = [string] "impression s counting \x13 \"you have"
      0001: t = s.split(' ')
      $[2] = [Array] [class: Array] {6}
      0001: t[2]
      $[3] = [string] "\x13"
      0001: t[2] * 1
      $[4] = [double] NaN
      0001: t[2].length
      $[5] = [integer] 1
      0001: t[2].toString(16)
      $[8] = [string] "\x13"
      0001: var u = '\x13';
      $[9] = [void] void
      0001: u.length
      $[10] = [integer] 1
      0001: s.indexOf(u)
      $[11] = [integer] -1
      0001: s
      $[12] = [string] "impression s counting \x13 \"you have"
      0001: s.indexOf('c')
      $[13] = [integer] 12
      0001: s.indexOf('\x13 ')
      $[14] = [integer] -1
      0001: s.indexOf(u)
      $[15] = [integer] -1
      0001: var v = "impression s counting \x13 \"you have"
      $[16] = [void] void
      0001: v.indexOf('\x13 ')
      $[17] = [integer] 21
      0001: u == t[2]
      $[18] = [boolean] false

      Comment

      • metamind
        New Member
        • Oct 2007
        • 3

        #4
        just tried escaping them. They are different (t[2] is the \x13 from the s while u has been set to \x13 in the code:

        0001: escape(t[2])
        $[21] = [string] "%u2013"
        0001: escape(u)
        $[22] = [string] "%13"

        Comment

        Working...