Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

javascript regex problem - this should be so simple!

Status
Not open for further replies.

mintymoke

Technical User
Nov 27, 2007
1
GB
Hi there,

I'm so frustrated! I am trying to get what I think should be a simple regular expression working, but it's evading me. In perl it would be

Code:
my $myString = "blah,blah,blah";
if ($myString ~= m/\,/) {deal with as an array}

in javascript from what I have read match() or test() would be appropriate methods. All I need to do is check for the existance of a comma in a string so I can deal with the string as an array.


Code:
var myString = "blah,blah,blah";
var re = new RegExp(/,/); // commas are not treated as specials apparantly so no escaping is needed
if (myString.match(re)) {alert("this is an array!");}

this returns myString.match() is not a function

I know that it's probably something really simple so apologies. I have searched and searched to find an answer but to no avail, I'm only testing in Firefox at present.

thanks

A
 
What went wrong is this.
[tt]//either
var re = new RegExp(",");
//or
var re=/,/;
//If you want full matches (returning both commas as match)
var re=new RegExp(",","g");
//or
var re=/,/g;
[/tt]
 
Further note
If applying split method is intended, it is this.
[tt] var re=/,/;
var arr=myString(re);
[/tt]
 
correction
Forgotten to put up the split part.
[tt] var arr=myString[red].split[/red](re);[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top