Skip to main content
K.Daube
Community Expert
Community Expert
April 13, 2015
Answered

Regex problem

  • April 13, 2015
  • 1 reply
  • 2269 views

Dear friends,

My project again is stuck in a hopefully trivial problem. Until now I searched for items in doulbe brackets, such as [[Dante, #712]]. But this form is somewaht superficial. When using EndNote or Citavi to insert placeholder citations, the form is with braces: {Dante, #712}.

Why does my regex in GetTempCitations find nothing?

I have tested this with RegexBuddy and in my standard program editor (EditPad Pro), where the items are found.

var txt1 = "Hello fans {Dante, #712} and just {some words} and another one {DuçanÌsídõrâ, #312}.";
    GetTempCitations (txt1);

var txt2 =  "introduction to [Content with square brackets] and he rest of something"; 
    otherTest (txt2);

function GetTempCitations (pgfText) {
  var regex = /{([^}]+)}/g;                       // g for global find

  while (result = pgfText.match (regex)) {
    if (result != null) {
      alert (pgfText.match (regex)[1]); 
    }
  }
}

function otherTest (pgfText) {
var regex = /\[([^\]]+)\]/; 
 
  if (pgfText.match (regex) !== null) { 
  alert (pgfText.match (regex)[1]); 
  }   
}

This topic has been closed for replies.
Correct answer JoH

The RegEx should probably be one of these two:

/\{([^\}]+)\}/g

/\{([^}]+)\}/g

Extendscript requires curly brackets to be escaped in order to be taken literally. Inside the character group, the backslash before the closing curly bracket is optional.

(It surprises me a little that the RegEx tester didn't complain.)

var txt1 = "Hello fans {Dante, #712} and just {some words} and another one {DuçanÌsídõrâ, #312}."; 

    GetTempCitations (txt1); 

 

var txt2 =  "introduction to [Content with square brackets] and he rest of something";   

    otherTest (txt2); 

 

function GetTempCitations (pgfText) { 

  var regex = /\{([^\}]+)\}/g;                       // g for global find 

  result = pgfText.match (regex);

  if (result != null) {

    for (var i=0; i < result.length; i++) {

      alert (result);

    }

  } 

 

function otherTest (pgfText) { 

var regex = /\[([^\]]+)\]/;   

   

  if (pgfText.match (regex) !== null) {   

  alert (pgfText.match (regex)[1]);   

  }     

1 reply

JoHCorrect answer
Inspiring
April 13, 2015

The RegEx should probably be one of these two:

/\{([^\}]+)\}/g

/\{([^}]+)\}/g

Extendscript requires curly brackets to be escaped in order to be taken literally. Inside the character group, the backslash before the closing curly bracket is optional.

(It surprises me a little that the RegEx tester didn't complain.)

var txt1 = "Hello fans {Dante, #712} and just {some words} and another one {DuçanÌsídõrâ, #312}."; 

    GetTempCitations (txt1); 

 

var txt2 =  "introduction to [Content with square brackets] and he rest of something";   

    otherTest (txt2); 

 

function GetTempCitations (pgfText) { 

  var regex = /\{([^\}]+)\}/g;                       // g for global find 

  result = pgfText.match (regex);

  if (result != null) {

    for (var i=0; i < result.length; i++) {

      alert (result);

    }

  } 

 

function otherTest (pgfText) { 

var regex = /\[([^\]]+)\]/;   

   

  if (pgfText.match (regex) !== null) {   

  alert (pgfText.match (regex)[1]);   

  }     

K.Daube
Community Expert
K.DaubeCommunity ExpertAuthor
Community Expert
April 13, 2015

Thank You Joh,

However, the probelm is: wich RegEx flavour is active in Escript?

I have tested against PowerGrep, Gnu ERE, JavaScript (MSIE Standard), Perl 5.20:

In Perl the braces enclose a match counter, e.g. {n} Match exactly n times. But there are exceptions to this: If a curly bracket occurs in any other context and does not form part of a backslashed sequence like \x{...} , it is treated as a regular character. In particular, the lower quantifier bound is not optional, and a typo in a quantifier silently causes it to be treated as the literal characters.

RegexBuddy allows to test in about 40 different flavours of RegEx syntax. So I assumed to be on the safe side...

Where are the details of RegEx syntax for ExtendScript (and the flavours used in FrameMaker which use the Boost library) documented?

For FM I found this: http://www.boost.org/doc/libs/1_34_0/libs/regex/doc/syntax_perl.html

But for ExtendScript ?

frameexpert
Community Expert
Community Expert
April 13, 2015

Hi Klaus, ExtendScript is just using the standard JavaScript regular expressions syntax. It has no relation to the Regular Expressions libraries built into FrameMaker 12. -Rick

www.frameexpert.com