These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

java script and regular expressions

alkaest2002
Registered: Dec 16 2010
Posts: 8

Hi everybody,
I am currently struggling with javascript and regular expressions.
My purpose is to count "M" letters not preceded by F in a target string (see below).
The regex pattern "[^F]M" I thought to use doesn't work (even though I have successfully tried it in a freeware that checks regular expressions against test strings); I had to change it to "\.M|M\."
I have also encountered problems using "^" and "$" as start and end locators (not in this specific example).
Am I doing something wrong with the javascript or does the adobe regular expression engine have specific limitations I am not aware of?
Thank you for your help and please forget my terrible English.
 
Pierpaolo
  
-------------- target string
FC.FY
FM.CF.m
M.FV.FT
Fr.FV.FY
m.C.F
FC'.M
CF.FM
m.CF
FM.C
  
--------------javascript code NOT working
var tot=0;
 
var s = this.getField("Blends").value;
 
var match = /[^F]M/.test(s);
if (match) {
tot = (s.match("[^F]M","g")).length;
}
 
event.value = tot;
-----------------------------------

My Product Information:
Acrobat Pro 9.0, Macintosh
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Regular Expressions in Acrobat JavaScript are identical to RegExps in any other implementation of JS, since they are part of the core functionality.
Here you can find out more about it:
http://www.w3schools.com/js/js_obj_regexp.asp
http://www.w3schools.com/jsref/jsref_obj_regexp.asp

As to your question. I would do it in the following way:

patt1 = /FM/g;
patt2 = /M/g;
tot = s.match(patt2).length - s.match(patt1).length;

There might be a way to combine both expressions into one, but that should work as well.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

alkaest2002
Registered: Dec 16 2010
Posts: 8
Dear try67,
thank you for your quick reply. your bit of code is really helpful.
Actually I did realize that the pattern "\.M|M\." select M but also instances of FM. My code to subtract those instances from the total (thus obtaining true Ms) is absolutely clumsier than yours.

Pierpaolo
try67
Expert
Registered: Oct 30 2008
Posts: 2398
The pattern you had, /[^F]M/g , works well in most cases. It doesn't work when you have an M at the begining of the string. I tried to write a pattern to treat that case as well, but then it occured to me that the subtraction method is easier. However, if someone is able to write such a pattern, I'd love to see how it's done.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

alkaest2002
Registered: Dec 16 2010
Posts: 8
The strange thing is that the software I use for checking patterns "sees" even the Ms at the beginning of lines with [^F]M. I have downloaded a new software which behaves differently, not recognizing Ms at the beginning of lines. Well, who knows...
Again thanks.