More regex woes

Suppose (this is a simplification) –

var aCompany = ['Smith-Kline', 'Smith-Burton', 'Williams-French'];
var testExp = new RegExp("/^" + companyName + "\-/i"); 
for(var i = 0; i < aCompany.length; i++) {
     if((aCompany[i]) && (aCompany.match(testExp))) {
           ...do something
     }
}

Although “Smith-Kline” should match on /^Smith-/i, it doesn’t in the code. Using the interactive console, however, I can get a true value on aCompany*.match(/^Smith-/i).*
I also tried defining testExp = “/^” + companyName + “-/i”; however, that didn’t work, either.
What will get this to work?
Thank you,
Don

When you use new RegExp, you shouldn’t include the surrounding slashes. Try this instead:

	var testExp = new RegExp("^" + companyName + "\-");
	testExp.ignoreCase = true;

Also, you forgot to add in the item you are matching. So the entire code should be:
```

  • var aCompany = [‘Smith-Kline’, ‘Smith-Burton’, ‘Williams-French’];
    var testExp = new RegExp(“^” + companyName + “-”);
    testExp.ignoreCase = true;
    for(var i = 0; i < aCompany.length; i++) {
    if((aCompany[i]) && (aCompany[i].match(testExp))) {
    …do something
    }
    }*
    ```

Thank you Joas!

Don

A follow up on this –

When I try passing a uid to a method,

var recordExp = new RegExp(record_uid.toString());
recordExp.ignoreCase = true;

If i test it in the interactive console, Servoy appears to not honor the ignoreCase request. A match() later in the code fails even though the only difference is case. However, it will work if I try instead to match on record_uid.toString().toLowerCase().

Anybody know about this?

Thank you,
Don

Does your uuid contain dashes “-” ? Because in that case the regexp may be something else than you think. You should escape all dashes with a slash: -

Hi Joas,

The UIDs are Servoy-assigned, and contain the dashes.

But when the UserManager plugin returns the record UIDs as a dataset of locked records, the UIDs are lowercase. I am trying to match up the returned UIDs with a particular record’s UID (to see who has locked the record).

So to make .match() work, I would need to replace each instance of a “-” with a “-” ?

Thank you,
Don

djlapin:
I would need to replace each instance of a “-” with a “-” ?

Yes, because if you have an uuid containing for example 1-4, then it doesn’t match the string “1-4”, but only the strings “1”, “2”, “3” and “4”. So that will give you strange results.

Thank you for explaining - very confusing for me.

Don

I think it will help you all lot to read a tutorial on regular expressions in javascript, for example this one.

I also like this regex chapter on the Mozilla Developer Network: Regular expressions - JavaScript | MDN

as well as this one: Intro to JavaScript | WebReference

and this online regulare expression tester can be handy too: JavaScript RegExp Example: Online Regular Expression Tester

Thanks, guys. It’s really like another language :o

My favorite comment on this subject is, “Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.”

Depending on the people I would say they have: (problem){0,2} ;)