Hitachi Vantara Pentaho Community Forums
Results 1 to 3 of 3

Thread: Replace function - problem with ? in parameter

  1. #1
    Join Date
    Apr 2008
    Posts
    18

    Default Replace function - problem with ? in parameter

    Hi - the replace function doesn't care for the leading ? in bold below. Using \ as an escape doesn't help - is there a different escape character, or a way around this? (3.1.0-RC1, Vista Business.)


    var str1 = "?careId=";

    var str2 = replace(str1,"careId=", "&careId=");
    Alert(str2);

    var str3 = replace(str2,"?&careId=", "?careId=");
    Alert(str3);


    Thanks
    -Kim

    Error message:

    Couldn't execute this script! Error on position (6:0) :
    Function call replace is not valid : Dangling meta character '?' near index 0
    ?&careId=
    ^ (script#6)

  2. #2
    DEinspanjer Guest

    Default

    replace is expecting a regular expression. ? is a special character in regular expressions. A single backslash won't work because you are using double quotes which means that Javascript will attempt to parse the backslash as a escape for a string character (such as \t or \n). You need two backslashes. \\ in a Java or Javascript string means "this is a literal backslash character", then, the literal backslash goes into the regular expression and the regex interprets that as "ignore the special meaning of the next character (e.g. the ?)".

    var str3 = replace(str2,"\\?&careId=", "?careId=");

  3. #3
    Join Date
    Mar 2008
    Posts
    1

    Default

    Thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Privacy Policy | Legal Notices | Safe Harbor Privacy Policy

Copyright © 2005 - 2019 Hitachi Vantara Corporation. All Rights Reserved.