473,480 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

searching a string surrounded by pipes using regular expressions

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
2 1531

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
62079
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I...
11
3856
by: Martin Robins | last post by:
I am trying to parse a string that is similar in form to an OLEDB connection string using regular expressions; in principle it is working, but certain character combinations in the string being...
7
9570
by: Ahmad A. Rahman | last post by:
Hi All, I have a string as and I want to split the string, which will turn into an array of: Arr = "1"; Arr = "two"; Arr = "three"; Arr = "fo""ur"; Arr = "fi,ve";
2
2015
by: ajitgoel | last post by:
Hi; I need some simple help with my regular expressions. I want to search my input text for all the boolean variables which do not start with bln. i.e I want to match "bool followed by 1 or...
32
14754
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
29
4273
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
7
2646
by: Brian Mitchell | last post by:
Is there an easy way to pull a date/time stamp from a string? The DateTime stamp is located in different parts of each string and the DateTime stamp could be in different formats (mm/dd/yy or...
2
1500
by: Bart Kastermans | last post by:
I have a file in which I am searching for the letter "i" (actually a bit more general than that, arbitrary regular expressions could occur) as long as it does not occur inside an expression that...
1
4225
by: Ben | last post by:
I apologize in advance for the newbie question. I'm trying to figure out a way to find all of the occurrences of a regular expression in a string including the overlapping ones. For example,...
0
7037
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7032
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6730
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5321
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4767
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2990
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
174
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.