473,405 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Regular Expression Question

Hi JavaScript Gurus,

I've a question on Regular Expressions using RegExp object. I just
want to know whether it is possible to do the search (see below) using
RegExp. Any pointers would be of immense help. Thanks.

My simple JavaScript code (inside a function):

=======================
var strList = "~@!~1#Apple~@!~2#Orange~@!~3#Mango~@!~4#Grapes~@! ~";
var strSearchStr = "~@!~3#(.[^~@!~]*)";
var objRegExp = new RegExp(strSearchStr, "gi");
var arrRegExp = objRegExp.exec(strStateList);
if (arrRegExp != null) {
document.write("<br>" + RegExp.$1);
}
delete(objRegExp);
return (true);
=======================

When I supply 3, I want to get the corresponding value that is Mango.
With the regular expression, I get the result I'm aiming for.

BUT...

if the list was changed to this:

var strList = "~@!~1#Apple~@!~2#Orange~@!~3#@!Mango~@!~4#Grapes~ @!~";

basically Mango has been changed to @!Mango. I've added two characters
@ and ! which are the row delimiters on the list string.

Then my regular expression search fails. The pattern I'm using [^~@!~]
searches and excludes individual characters ~, @ and !. But I want it
to exclude the grouping (~@!~).

I've tried the following reg exp. searches with no luck:

~@!~3#(.[^(~@!~)]*)
~@!~3#(.[^(~@!~){0,1}]*)

Any idea as what I'm doing wrong. I know this should be pretty simple
to implement.

Thanks,
Gopi
Jul 23 '05 #1
3 1500
Gopinath wrote:
[...]
var strList = "~@!~1#Apple~@!~2#Orange~@!~3#Mango~@!~4#Grapes~@! ~"; ^^^^^^^ var strSearchStr = "~@!~3#(.[^~@!~]*)";
var objRegExp = new RegExp(strSearchStr, "gi");
var arrRegExp = objRegExp.exec(strStateList); ^^^^^^^^^^^^
What is `strStateList'?
if (arrRegExp != null) {
if (arrRegExp)

suffices.
document.write("<br>" + RegExp.$1);
You should not use RegExp.$* anymore, those properties are deprecated.
RegExp.prototype.exec() returns the extended Array object you are looking
for -- use arrRegExp[1].
}
delete(objRegExp);
`delete' is not a method, it is a special operator. You would have removed
the parantheses and include whitespace between operator and operand if
the `delete' operation would have made sense: You can only apply `delete'
successfully on variables that have *not* been declared with `var' or on
object properties. But since `objRegExp' is declared local and does not
exist outside of the execution context, it is not required to free the
allocated memory explicitely.
return (true);
`return' is not a method, it is a something between statement and operator.
It is not necessary to use parantheses here.
=======================

When I supply 3, I want to get the corresponding value that is Mango.
With the regular expression, I get the result I'm aiming for.
Which is surprising to me.
BUT...

if the list was changed to this:

var strList = "~@!~1#Apple~@!~2#Orange~@!~3#@!Mango~@!~4#Grapes~ @!~";

basically Mango has been changed to @!Mango. I've added two characters
@ and ! which are the row delimiters on the list string.

Then my regular expression search fails. The pattern I'm using [^~@!~]
searches and excludes individual characters ~, @ and !.
Which is why the second "~" can be removed.
But I want it to exclude the grouping (~@!~).

I've tried the following reg exp. searches with no luck:

~@!~3#(.[^(~@!~)]*)
~@!~3#(.[^(~@!~){0,1}]*)
Within a character class, with the exception of "^" at the beginning
and "-" between other characters, characters have no special meaning,
so the above character classes will only additionally match "(", "~",
")" and "{", "0", ",", "1", and "}".
Any idea as what I'm doing wrong.
You have not read the manual.

<http://devedge.netscape.com/library/manuals/2000/javascript/1.5/guide/>
<http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/>
<http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/regexp.html#1193136>
I know this should be pretty simple to implement.


var strSearchStr = "~@!~3#(.(?!^~@!~)*)";

But if you *know* which RegExp to use, you should not use the RegExp()
constructor, but RegExp object literals:

var
strList = "~@!~1#Apple~@!~2#Orange~@!~3#Mango~@!~4#Grapes~@! ~",
arrRegExp = /~@!~3#(.(?!~@!~)*)/gi.exec(strList);
...
HTH

PointedEars
Jul 23 '05 #2

Hi PointedEars,

Thanks for the suggestions and pointers. They were helpful. And I've
found out the right regular expression for my problem.

Thanks,
Gopi
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #3
Alien Visitor wrote:
Thanks for the suggestions and pointers. They were helpful.
You're welcome.
And I've found out the right regular expression for my problem.


Care to give something back to the community?
PointedEars

P.S.: Changing names on Usenet is undesired behavior.
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Vibha Tripathi | last post by:
Hi Folks, I put a Regular Expression question on this list a couple days ago. I would like to rephrase my question as below: In the Python re.sub(regex, replacement, subject)...
5
by: Bradley Plett | last post by:
I'm hopeless at regular expressions (I just don't use them often enough to gain/maintain knowledge), but I need one now and am looking for help. I need to parse through a document to find a URL,...
10
by: Lee Kuhn | last post by:
I am trying the create a regular expression that will essentially match characters in the middle of a fixed-length string. The string may be any characters, but will always be the same length. In...
18
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
5
by: Ryan | last post by:
HELLO I am using the following MICROSOFT SUGGESTED (somewhere on msdn) regular expression to validate email addresses however I understand that the RFP allows for "+" symbols in the email address...
7
by: norton | last post by:
Hello, Does any one know how to extact the following text into 4 different groups(namely Date, Artist, Album and Quality)? - Artist - Album Artist - Album - Artist - Album - Artist -...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
6
by: Ludwig | last post by:
Hi, i'm using the regular expression \b\w to find the beginning of a word, in my C# application. If the word is 'public', for example, it works. However, if the word is '<public', it does not...
3
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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,...
0
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
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...

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.