0
[JS] Reg Exp Error when using *

/t5/indesign-discussions/js-reg-exp-error-when-using/td-p/1100064
Oct 09, 2008
Oct 09, 2008
Copy link to clipboard
Copied
Hello everyone,
I get a syntax error when executing a script which contains RegExp handling strings with "*".
ie:
var x = /\*\*\*ABC/;
alert (RegExp(x)); //*** WORKS because x is defined as an EXPRESSION
var x = "\*\*\*ABC";
alert (RegExp(x)); //*** SYNTAX ERROR when executing
var x = "***ABC";
alert (RegExp(x)); //*** SYNTAX ERROR when executing
My problem is I have to build the expression using strings. So I was hoping to concatenate the string portionsa and use New RegExp to create my expression. Unfortunately, if we have STARS (*) in the string you get an error. Which as forced me to HARDCODE and I whish not to.
ie:
alert (yyy.search(/\*\*\*ABC/);
FYI: I have expample in HTML where ".value" is used. But it does not work w/ InDesign???
ie:
var re = new RegExp(document.demoMatch.regex.value);
if (document.demoMatch.subject.value.match(re))
alert("Successful match");
else
alert("No match");
Any suggestions?
I get a syntax error when executing a script which contains RegExp handling strings with "*".
ie:
var x = /\*\*\*ABC/;
alert (RegExp(x)); //*** WORKS because x is defined as an EXPRESSION
var x = "\*\*\*ABC";
alert (RegExp(x)); //*** SYNTAX ERROR when executing
var x = "***ABC";
alert (RegExp(x)); //*** SYNTAX ERROR when executing
My problem is I have to build the expression using strings. So I was hoping to concatenate the string portionsa and use New RegExp to create my expression. Unfortunately, if we have STARS (*) in the string you get an error. Which as forced me to HARDCODE and I whish not to.
ie:
alert (yyy.search(/\*\*\*ABC/);
FYI: I have expample in HTML where ".value" is used. But it does not work w/ InDesign???
ie:
var re = new RegExp(document.demoMatch.regex.value);
if (document.demoMatch.subject.value.match(re))
alert("Successful match");
else
alert("No match");
Any suggestions?
TOPICS
Scripting
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Community Expert
,
/t5/indesign-discussions/js-reg-exp-error-when-using/m-p/1100065#M337329
Oct 09, 2008
Oct 09, 2008
Copy link to clipboard
Copied
Let's see if I got this right... So what you're asking is how to look for an asterisk in a string, using a RegExp?
If that's the question, the answer is that you need to double-escape it, because it's a special character, like so:
var a="\\*";
var patt1=new RegExp(a);
alert(patt1.test("abcd*efg"));
The result should be "true".
If that's the question, the answer is that you need to double-escape it, because it's a special character, like so:
var a="\\*";
var patt1=new RegExp(a);
alert(patt1.test("abcd*efg"));
The result should be "true".
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

Guest
AUTHOR
/t5/indesign-discussions/js-reg-exp-error-when-using/m-p/1100066#M337331
Oct 09, 2008
Oct 09, 2008
Copy link to clipboard
Copied
Yes it works!!!
To look for 3 stars and "ABC", I have to define like this
var a="\\*\\*\\*ABC";
var patt1=new RegExp(a);
alert(patt1.test("abcd*ABCefg"));
So when I build the string I have to use 2 backslashes!!!
Thank you so much.
FYI: Using ".source" work too. Still working on how to add a flag (/i)
var y = "xxx***ABCyyyxxx***ABCyyyxxx***ABCyyy";
var x = /\*\*\*/;
var x = new RegExp(x.source + "ABC");
alert (RegExp(x));
var z = x.exec(y);
if (z != null)
alert ("Found at position "+z.index+"\nHow many: "+z.length);
var z = y.match(x);
if (z != null)
alert ("Found at position "+z.index+"\nHow many: "+z.length);
To look for 3 stars and "ABC", I have to define like this
var a="\\*\\*\\*ABC";
var patt1=new RegExp(a);
alert(patt1.test("abcd*ABCefg"));
So when I build the string I have to use 2 backslashes!!!
Thank you so much.
FYI: Using ".source" work too. Still working on how to add a flag (/i)
var y = "xxx***ABCyyyxxx***ABCyyyxxx***ABCyyy";
var x = /\*\*\*/;
var x = new RegExp(x.source + "ABC");
alert (RegExp(x));
var z = x.exec(y);
if (z != null)
alert ("Found at position "+z.index+"\nHow many: "+z.length);
var z = y.match(x);
if (z != null)
alert ("Found at position "+z.index+"\nHow many: "+z.length);
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Contributor
,
/t5/indesign-discussions/js-reg-exp-error-when-using/m-p/1100067#M337332
Oct 09, 2008
Oct 09, 2008
Copy link to clipboard
Copied
Yes, the string should be defined with double-escaped asterix:
edit:
Ah, you have got it.
> Still working on how to add a flag (/i)
Try it like this:
Martin
var x = "xxx\\*\\*\\*ABCzzz";
alert (RegExp(x));
edit:
Ah, you have got it.
> Still working on how to add a flag (/i)
Try it like this:
alert (RegExp(x, 'i'));
Martin
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

Guest
AUTHOR
/t5/indesign-discussions/js-reg-exp-error-when-using/m-p/1100068#M337333
Oct 09, 2008
Oct 09, 2008
Copy link to clipboard
Copied
Yes Sir. I was about to reply.
You can't imagine how much HARDCODING I can replace now.
Thank you a LOT.
You can't imagine how much HARDCODING I can replace now.
Thank you a LOT.
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Contributor
,
/t5/indesign-discussions/js-reg-exp-error-when-using/m-p/1100069#M337335
Oct 09, 2008
Oct 09, 2008
Copy link to clipboard
Copied
Alexandre,
this morning I had to crack those RegExp-nuts by myself.
So I have been surprised just now reading my private question public signed by the name of somebody else. ;-)
Martin
this morning I had to crack those RegExp-nuts by myself.
So I have been surprised just now reading my private question public signed by the name of somebody else. ;-)
Martin
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Community Expert
,
LATEST
/t5/indesign-discussions/js-reg-exp-error-when-using/m-p/1100070#M337336
Oct 09, 2008
Oct 09, 2008
Copy link to clipboard
Copied
Glad to have been of help to you...
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

