Connecting Tech Pros Worldwide Forums | Help | Site Map

Regular Expression Question

Gopinath
Guest
 
Posts: n/a
#1: Jul 23 '05
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

Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Regular Expression Question


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

if (arrRegExp)

suffices.
[color=blue]
> document.write("<br>" + RegExp.$1);[/color]

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].
[color=blue]
> }
> delete(objRegExp);[/color]

`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.
[color=blue]
> return (true);[/color]

`return' is not a method, it is a something between statement and operator.
It is not necessary to use parantheses here.
[color=blue]
> =======================
>
> 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.[/color]

Which is surprising to me.
[color=blue]
> 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 !.[/color]

Which is why the second "~" can be removed.
[color=blue]
> But I want it to exclude the grouping (~@!~).
>
> I've tried the following reg exp. searches with no luck:
>
> ~@!~3#(.[^(~@!~)]*)
> ~@!~3#(.[^(~@!~){0,1}]*)[/color]

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 "}".
[color=blue]
> Any idea as what I'm doing wrong.[/color]

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>
[color=blue]
> I know this should be pretty simple to implement.[/color]

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
Alien Visitor
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Regular Expression Question



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!
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Regular Expression Question


Alien Visitor wrote:
[color=blue]
> Thanks for the suggestions and pointers. They were helpful.[/color]

You're welcome.
[color=blue]
> And I've found out the right regular expression for my problem.[/color]

Care to give something back to the community?


PointedEars

P.S.: Changing names on Usenet is undesired behavior.
Closed Thread


Similar JavaScript / Ajax / DHTML bytes