Javascript .match not working on text box input

Comments

5 comments

  • Stefan Ukena

    Hi Christian,

    I assume that your Podio text field is a multi-line text field, right? Those actually are internally stored as HTML, even if you are not using any formatting. In addition, Podio seems to perform some magic when you are using them inside a calculation, namely, converting them to markdown. (At least that's what I noticed last time I had to deal with this.) Long story short: your regex is not working, because the input string is not what you think it is.

    So, how can you figure out what's actually in that text field according to Podio? Output it as a code block in seperate calculation like so:

    "```\n" + @TextField + "```"
    

    This should give you the verbatim markdown of your text field. Note: those are not single quotation marks, but back ticks.

    Best,
    Stefan

    dieKollaborateure.com - Podio Training+Consulting+Developmentauch auf Deutsch

    0
    Comment actions Permalink
  • Hamid
    
    var re = /\d\d\d?\d,\d\d\d/;
    var comp = @Text;
    var m = re.exec(comp);
    var a = JSON.stringify(m)
    
    var result = a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8]
    result
    
    
    0
    Comment actions Permalink
  • Christian Austin

    Thanks Stefan and Hamid.

    Stefan, I was unable to get podio to spit out text any different form that in the text box. The output was simply text in the text box.

    Hamid, your solution worked, but I wanted to remain flexible to the number of digits in the number; so I used modified your solution to get the following hack to work.

    var re = /\$([0-9,]+)/;
    var comp = @Text;
    comp =comp.replace(/[^\$\w]/gi,'');
    var m = comp.match(re);
    var comp_str = JSON.stringify(m)
    f_ind = comp_str.indexOf(',')+2;
    l_ind = comp_str.slice(f_ind,comp_str.length).indexOf('"');
    comp_str.substr(f_ind,l_ind)

    Do you know why we have to JSON.stringify after matching to get an output that will not give an error? Does this have something to do with podio calculations not working with arrays of strings? It seems like there is a lot of extra code here that should not be necessary to handle parsing JSON stringify output.

    0
    Comment actions Permalink
  • Colin Brown

    I am having a similar problem, but cannot find a solution based on the answers here. (It also doesn't help that I'm a coding noob in general, so I beg forgiveness in advance for that.)
    Essentially i am trying to look for more than one instance of a certain string inside another string, where both of those strings come from referenced fields.
    My code (simplified) is:

    
    var patt = String(@Field1)
    var text = String(@All of Field2)
    var result = text.match(/patt/g)
    if (result.length > 1) {
    // do something
    }
    
    

    @Field1 references a single-line text field in the same app
    @All of Field2 references a calc field in another app, which returns a string (that calc is essentially @all of something.join(" ") )

    I've made sure that both the text and patt variables return strings and even put them in String() to make sure.
    I've also used Stefan's method here to verify that neither has any invisible markdown

    The problem with the code above is that the value of result is always null, even when I know the test data I have should produce matches.

    As far as i can tell, the problem lies in (/patt/g). It doesn't like using a variable as the regular expression pattern.

    However, if I remove the slashes and g modifier so that var result = text.match(patt) then result returns one instance of patt but result.length returns "Cannot read property 'length' of null". More to the point, this doesn't achieve my purpose, because i need to be able to check for more than one instance of patt , so I do need that regexp g modifier.

    Any suggestions?
    TIA!

    0
    Comment actions Permalink
  • Rainer Grabowski

    Hi Colin,

    I assume you want to know if var patt occours more then one time in var text, like:
    field1 = "Paul"
    @all of field2 = "Paul,Mary,John,Paul"
    If so, you can use:

    var patt = @Field1;
    var text = @All of Field2;
    var count = 0;
    for(var i = 0; i < text.length; ++i){
    if(text[i] == patt)
        count++;
    }
    count > 1 ?  "foo" : "bar"
    

    Result for "Paul" would be "foo"

    Rainer
    rg@delos-consulting.com

    0
    Comment actions Permalink

Please sign in to leave a comment.

Powered by Zendesk