R
Robert Oschler
One of the things that has always thrown me about Javascript regular
expression patterns, is that you don't include quotes around them. So how
can I modify a regex pattern at runtime. For example, given:
var regexPattern = /.*?\s/i;
Suppose I want to change the whitespace character to a comma, based on a
variable being true or false?
// This doesn't work, the string var double-quotes cause the match() method
to find nothing.
var delimChar = "/s";
if (commaDelimited)
delimChar = ",";
var regexPattern = "/.*?" + delimChar + "/i";
var matches = theString.match(regexPattern);
So what is the proper syntax?
Thanks.
expression patterns, is that you don't include quotes around them. So how
can I modify a regex pattern at runtime. For example, given:
var regexPattern = /.*?\s/i;
Suppose I want to change the whitespace character to a comma, based on a
variable being true or false?
// This doesn't work, the string var double-quotes cause the match() method
to find nothing.
var delimChar = "/s";
if (commaDelimited)
delimChar = ",";
var regexPattern = "/.*?" + delimChar + "/i";
var matches = theString.match(regexPattern);
So what is the proper syntax?
Thanks.