Legacy Dev Forum Posts

 View Only

Sign Up

Strip spaces trailing spaces, carriage returns, tabs from input variable?

  • 1.  Strip spaces trailing spaces, carriage returns, tabs from input variable?

    Posted 06-05-2025 18:44

    VaunMcCarthy | 2023-06-24 02:36:48 UTC | #1

    Since scripter is a bit limited in what you can really do with strings, can you do things like nested .Replace when calling an input contract variable and have it strip out trailing spaces (but to leave a middle space such as in "John Smith"), carriage returns and tabs?


    Jason_Mathison | 2023-06-26 14:02:56 UTC | #2

    You have access to fairly powerful string tools, like this one: https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html#replaceAll(java.lang.String,java.lang.String)

    --Jason


    Artur_Dishunts | 2023-07-03 02:32:52 UTC | #3

    Hi Jason,

    I was trying to remove trailing and leading spaces using this regex expression: "\A\s+|\s+\Z.". I tested it on regex101.com and it returned spaces only at the beginning and at the very end. When I tried to do this in successTemplate using replaceAll, it didn't work. Do you know what I'm doing wrong?

    Below is my response configuration:

    {
      "translationMap": {
        "name": "$.entities[*].name",
        "content": "$.entities[*].texts[0].content"
      },
      "translationMapDefaults": {
        "name": "\"\"",
        "content": "[]"
      },
      "successTemplate": "{ \"content\" : $content.replaceAll(\"</p>|<br />\",'\\\\n').replaceAll(\"<[^>]+>\", 
    \"\").replaceAll(\"&nbsp;\", \" \").replaceAll(\"&amp;\", \"&\").replaceAll(\"&ldquo;\", \"“\").replaceAll(\"&rdquo;\", 
    \"”\").replaceAll(\"&lsquo;\", \"‘\").replaceAll(\"&rsquo;\", \"’\").replaceAll(\"&ndash;\", \"–
    \").replaceAll(\"&mdash; \", \"—\").replaceAll(\"&ntilde;\", \"ñ\").replaceAll(\"&aacute;\", 
    \"á\").replaceAll(\"&eacute;\", \"é\").replaceAll(\"\\\\A\\\\s+|\\\\s+\\\\Z\",\"\"), \"name\" : $name }"
    }


    Jason_Mathison | 2023-07-08 04:21:40 UTC | #4

    Hi Artur,

    I worked through this issue with a response of:

    {
    "foo" : "     bar   "
    }

    In your example you use \A and \Z which are assertions. It seems to work in the regex101 tester, but I have never tried to use them in a successTemplate. Switching to ^ and $ seems to work. Finally, I had to account for there being a quote at the start and end of the variable, so I replace the double quote followed by whitespace with a double quote.

    {
      "translationMap": {
        "foo": "foo"
      },
      "translationMapDefaults": {},
      "successTemplate": "{ \"foo\" : $foo.replaceAll(\"^$esc.quote\\s+|\\s+$esc.quote$\", \"$esc.quote\")}"
    }

    --Jason


    Artur_Dishunts | 2023-07-12 14:55:40 UTC | #5

    Thanks Jason, I tried it but for some reason it didn't work for me, I found how to remove space in the beginning using .replaceAll(\"<div[^>]+>\\\\n\",\"\") and spaces at end in Genesys Script


    Jason_Mathison | 2023-07-12 16:11:43 UTC | #6

    Is there anything else you need a this point?


    Artur_Dishunts | 2023-07-13 12:41:47 UTC | #7

    Nope, I am good thanks!


    system | 2023-08-13 12:42:20 UTC | #8

    This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.


    This post was migrated from the old Developer Forum.

    ref: 20656