Hitachi Vantara Pentaho Community Forums
Results 1 to 3 of 3

Thread: Javascript and str2RegExp

  1. #1

    Default Javascript and str2RegExp

    Hi!

    I tried to use the str2RegExp javascript function to test if a string ends with the character "A" and if so remove it:
    Code:
    var dest = destination.getString();
    var strReg = "^(.*)A$";
    var res =  str2RegExp(dest, strReg);
    
    if (res.length > 0){
        type.setValue("val1");
        destination.setValue(res[0]);
    } else {
        type.setValue("val2");
    }
    But when I test the script I got:
    Code:
    TypeError: Cannot read property "length" from null
    I can't find any documentation on this function. Any help or exemple is welcome.

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

    Default

    try length() but first try to find out why res is null afterwards

    Regards,
    Sven

  3. #3

    Default

    No, there is no parenthesis.
    There is no doc in Spoon Documention but I found an exemple here: http://kettle.pentaho.org/svn/Kettle...nctionHelp.xml

    Code:
    var strToMatch = "info@proconis.de";
    var strReg = "^(\\w+)@([a-zA-Z_]+?)\\.([a-zA-Z]{2,3})$";
    var xArr =  str2RegExp(strToMatch, strReg);
    for(i=0;i<xArr.length;i++){
        Alert(xArr[i]);
    }
    This exemple is working.

    I correct my code because if the regexp doesn't match, res is null and length isn't define:
    Code:
    var dest = destination.getString();
    var strReg = "^(\\w+)A$";
    var res =  str2RegExp(dest, strReg);
    
    if (res){
        type.setValue("val1");
        destination.setValue(res[0]);
    } else {
        type.setValue("val2");
    }
    It's working.

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.