Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

[JS] Reg Exp Error when using *

Guest
Oct 09, 2008 Oct 09, 2008
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?
TOPICS
Scripting
670
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 09, 2008 Oct 09, 2008
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".
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 09, 2008 Oct 09, 2008
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);
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Oct 09, 2008 Oct 09, 2008
Yes, the string should be defined with double-escaped asterix:


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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 09, 2008 Oct 09, 2008
Yes Sir. I was about to reply.

You can't imagine how much HARDCODING I can replace now.

Thank you a LOT.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Oct 09, 2008 Oct 09, 2008
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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 09, 2008 Oct 09, 2008
LATEST
Glad to have been of help to you...
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines