Hitachi Vantara Pentaho Community Forums
Results 1 to 9 of 9

Thread: Am I losing it? Weird result from replace command

  1. #1
    Join Date
    Feb 2008
    Posts
    216

    Default Am I losing it? Weird result from replace command

    Hello -
    I have code where I'm trying to trim out all alpha characters, plus a couple of special characters, from a string field. However, my result is an empty string, even though there should be numbers left in the result.

    I've attached a screenshot of what I'm getting.

    Here's the code:

    var order_reference_number1 = trim(reference.getString());
    var order_reference_number = undefined;

    order_reference_number = trim(replace(order_reference_number1,
    "A", "",
    "B", "",
    "C", "",
    "D", "",
    "E", "",
    "F", "",
    "G", "",
    "H", "",
    "I", "",
    "J", "",
    "K", "",
    "L", "",
    "M", "",
    "N", "",
    "O", "",
    "P", "",
    "Q", "",
    "R", "",
    "S", "",
    "T", "",
    "U", "",
    "V", "",
    "W", "",
    "X", "",
    "Y", "",
    "Z", "",
    "a", "",
    "b", "",
    "c", "",
    "d", "",
    "e", "",
    "f", "",
    "g", "",
    "h", "",
    "i", "",
    "j", "",
    "k", "",
    "l", "",
    "m", "",
    "n", "",
    "o", "",
    "p", "",
    "q", "",
    "r", "",
    "s", "",
    "t", "",
    "u", "",
    "v", "",
    "w", "",
    "x", "",
    "y", "",
    "z", "",
    "-", "",
    "/", "",
    ".", ""));
    Attached Images Attached Images  

  2. #2
    Join Date
    May 2006
    Posts
    4,882

    Default

    Yikes... you're losing it . Raise a JIRA at http://jira.pentaho.org/browse/JIRA : nothing stands out, but possibly you're hitting a limit somewhere (even so ... you're version would very slow).

    Try something as:

    var str1 = "Hello World 123, this is a nice function";
    var str2 = replace(str1,"\\D", "");
    Alert(str2);

    to remove all non-digits from a string... Patterns that can be used e.g. on http://java.sun.com/javase/6/docs/ap...x/Pattern.html and note you have to escape \'s

    Regards,
    Sven

  3. #3
    Join Date
    Nov 1999
    Posts
    9,729

    Default

    Well, in good JavaScript tradition replace uses regular expressions to to match, as such the statement becomes:

    Code:
    var stripped = replace(original, "[a-z]", "", "[A-Z]", "", "\\.", "", " ", "");
    Matt

  4. #4
    Join Date
    Nov 1999
    Posts
    9,729

    Default

    Forget it, Sven's solution is better ;-)

  5. #5
    Join Date
    May 2006
    Posts
    4,882

    Default

    For the original poster, the "." is the one erasing your string... it means any character; "\\." matches a regular "dot".

    Regards,
    Sven

  6. #6
    Join Date
    Feb 2008
    Posts
    216

    Default Thank you!!!

    Quote Originally Posted by sboden View Post
    For the original poster, the "." is the one erasing your string... it means any character; "\\." matches a regular "dot".

    Regards,
    Sven
    It was really driving me crazy, because when I did a subset of characters it worked fine. This is good to know.

  7. #7
    Join Date
    Feb 2008
    Posts
    216

    Default \\D works like a charm!

    Thanks Sven! I think I owe you two beverages of your choice now.

  8. #8

    Default

    Hi,

    Since 3.1, we had the getDigitsOnly function in Java Script Mod :-)

    See picture


    Samatar
    Last edited by shassan2; 03-31-2009 at 08:31 AM.
    Samatar

  9. #9
    Join Date
    Feb 2008
    Posts
    216

    Default Very cool... I'm not fully on 3.1 yet

    Quote Originally Posted by shassan2 View Post
    Hi,

    Since 3.1, we had the getDigitsOnly function in Java Script Mod :-)

    See picture


    Samatar
    There are a couple of issues I'm still working around on that version.

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.