Skip to main content
Inspiring
February 17, 2017
Answered

can string.match() find more than one match?

  • February 17, 2017
  • 2 replies
  • 2522 views

I don't seem to get the string.match() function right. What I want for it is to return an array with all the matches found but it only seems to return the first one. Is this how it's supposed to work and can anyone suggest another function or a workaround for me? I need to know if there is more than one occurrence of the pattern searched for.

E.g. the code below I'd like to return an array of two matches:

var keyword = "see";

var str = "Let's see if you can see me";

var match = str.match(new RegExp(keyword)); //match = see

I'll be making tens of thousands of these calls for the project I'm working on so it's good if its fairly efficient. The only workaround I can think of is using findGrep() instead, but it seems a little over the top for this?

This topic has been closed for replies.
Correct answer Jongware

That is because you did not supply the find global flag "g" to the regex constructor.

See Adobe InDesign CS6 (8.0) Object Model JS: RegExp (which ought to mirror what is said in the more official JavaScript reference at String.prototype.match() - JavaScript | MDN ).

This will return an array of length 2:

var keyword = "see";

var str = "Let's see if you can see me";

var match = str.match(new RegExp(keyword, "g")); //match = see

alert (match)

Whether or not ID's native findGrep is faster depends on where you get your strings from and if you want to keep a link to the actual formatted text alive. (It usually is not – I've found JS's internal string handling much, much faster than when continuously interacting with ID.)

2 replies

Inspiring
February 17, 2017

Great! Thanks for pointing it out. I was actually looking at the Javascript reference but I didn't notice that difference and thought the difference was down to Adobe's functions being slightly different from the javascript functions. (I noticed that javascript has a .exec() function which is not available in inDesign script)

Jongware
JongwareCorrect answer
Community Expert
February 17, 2017

That is because you did not supply the find global flag "g" to the regex constructor.

See Adobe InDesign CS6 (8.0) Object Model JS: RegExp (which ought to mirror what is said in the more official JavaScript reference at String.prototype.match() - JavaScript | MDN ).

This will return an array of length 2:

var keyword = "see";

var str = "Let's see if you can see me";

var match = str.match(new RegExp(keyword, "g")); //match = see

alert (match)

Whether or not ID's native findGrep is faster depends on where you get your strings from and if you want to keep a link to the actual formatted text alive. (It usually is not – I've found JS's internal string handling much, much faster than when continuously interacting with ID.)

Community Expert
February 17, 2017

Hi together,

FWIW: Sometimes InDesign's findGrep is easier to handle.

Especially when it comes to complex layout structures where nested text frames, footnotes and nested tables are all over the place. Also in case MultiStateObjects with several states are present that could contain text frames you want to inspect.

Regards,
Uwe