Javascript .match not working on text box input
I am trying to extract a substring from a text box input. The code is as follows
var re = /.*\$([0-9,\s]+).*Price/mi;
var comp = '02148$379,900Listed at Price3Beds2Baths1,245Sq.Ft.$305 / Sq. Ft.Built: 2006Cumulative: 26 daysOn Redfin: 26 days'
var comp1 = @Text;
var m = comp.match(re);
m[1]
the output of m[1] is 379,900, and is correct for the hard coded string comp. However, when I input the same string into the text box @Text in my app, and switch comp.match(re) to comp1.match(re) in the code above, I receive the error
Script Error: Type Error Cannot read property '1' of null
Any suggestions?
-
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,
StefandieKollaborateure.com - Podio Training+Consulting+Development — auch auf Deutsch
-
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.
-
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 markdownThe 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 thatvar result = text.match(patt)
thenresult
returns one instance ofpatt
butresult.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 ofpatt
, so I do need that regexpg
modifier.Any suggestions?
TIA! -
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
Please sign in to leave a comment.
Comments
5 comments