By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,636 Members | 1,190 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,636 IT Pros & Developers. It's quick & easy.

searching a string surrounded by pipes using regular expressions

P: n/a
hello. this is all the code in my javascript. there's nothing else in
the html page except for the following:

var regex = new RegExp("\|A\|");
if (regex.test("8_2006|13|15|31|"))
alert("success");
else
alert("fail");

if i run the above code, i get the "success" message box, but shouldn't
the "fail" message appear instead? the regular expression is defined
as the character A surrounded by a pipe on each end, but there's no
instance of that expression in my string when i perform the test. can
someone please help? thanks

Sep 18 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

silly putty wrote:
hello. this is all the code in my javascript. there's nothing else in
the html page except for the following:

var regex = new RegExp("\|A\|");
When constructing regular expressions that way, you need to
double-quote special characters that you want to quote:

var regex = new RegExp("\\|A\\|");

Typically the middle part will provided by a variable:

var txt = 'A';
var regex = new RegExp('\\|' + txt + '\\|");

if (regex.test("8_2006|13|15|31|"))
You can also do this test using:

if ( /\|A\|/.test("8_2006|13|15|31|")) {
/* expression is true */
} else {
/* expression is false */
}
--
Rob

Sep 19 '06 #2

P: n/a
that works perfectly. thanks alot!

RobG wrote:
silly putty wrote:
hello. this is all the code in my javascript. there's nothing else in
the html page except for the following:

var regex = new RegExp("\|A\|");

When constructing regular expressions that way, you need to
double-quote special characters that you want to quote:

var regex = new RegExp("\\|A\\|");

Typically the middle part will provided by a variable:

var txt = 'A';
var regex = new RegExp('\\|' + txt + '\\|");

if (regex.test("8_2006|13|15|31|"))

You can also do this test using:

if ( /\|A\|/.test("8_2006|13|15|31|")) {
/* expression is true */
} else {
/* expression is false */
}
--
Rob
Sep 21 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.