473,396 Members | 2,106 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,396 software developers and data experts.

RegEx question

CB
Trying to match the entire following object literal code using a RegEx.
var Punctuators = { '{' : 'LeftCurly', '}' : 'RightCurly' }

Variations on the idea of using /var.*{.*}/ of course stops at the
first }. Any ideas?
Thanks in advance.

Jun 15 '06 #1
7 1512
CB wrote on 15 jun 2006 in comp.lang.javascript:
Trying to match the entire following object literal code using a RegEx.
var Punctuators = { '{' : 'LeftCurly', '}' : 'RightCurly' }


/var Punctuators = \{ \'\{\' : \'LeftCurly\', \'\}\' : \'RightCurly\' \}/
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 15 '06 #2
CB

Evertjan. wrote:
CB wrote on 15 jun 2006 in comp.lang.javascript:
Trying to match the entire following object literal code using a RegEx.
var Punctuators = { '{' : 'LeftCurly', '}' : 'RightCurly' }


/var Punctuators = \{ \'\{\' : \'LeftCurly\', \'\}\' : \'RightCurly\' \}/


Actually, my intention is to match everything within the { } of any
object literal. Considering that '}' string literals may be embedded.

Jun 15 '06 #3
CB wrote on 15 jun 2006 in comp.lang.javascript:

Evertjan. wrote:
CB wrote on 15 jun 2006 in comp.lang.javascript:
> Trying to match the entire following object literal code using a
> RegEx. var Punctuators = { '{' : 'LeftCurly', '}' : 'RightCurly' }
>


/var Punctuators = \{ \'\{\' : \'LeftCurly\', \'\}\' : \'RightCurly\'
\}/


Actually, my intention is to match everything within the { } of any
object literal. Considering that '}' string literals may be embedded.


RegEx matching can only be done on a string,
not on an object or on a any litteral.

Such string could be constructed from a string litteral,
but the matching is on the resulting string.

What has an object litteral to do with regEx?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 15 '06 #4
CB
> RegEx matching can only be done on a string,
not on an object or on a any litteral.

Such string could be constructed from a string litteral,
but the matching is on the resulting string.

What has an object litteral to do with regEx?


Sorry I'm not explaining this well. I'm only referring to matching a
string. The string just happens to be code for a obj literal. The
object that the literal describes is not relevant.

var a = "{ '{' : 'LeftCurly', '}' : 'RightCurly' }";
var b = "{ '{' : 'A punctator', '}' : 'Another punctuator' }";

I'm looking for the RegEx that will match both the entire strings in
the variables a and b.

Jun 15 '06 #5
CB wrote on 15 jun 2006 in comp.lang.javascript:
RegEx matching can only be done on a string,
not on an object or on a any litteral.

Such string could be constructed from a string litteral,
but the matching is on the resulting string.

What has an object litteral to do with regEx?


Sorry I'm not explaining this well. I'm only referring to matching a
string. The string just happens to be code for a obj literal. The
object that the literal describes is not relevant.

var a = "{ '{' : 'LeftCurly', '}' : 'RightCurly' }";
var b = "{ '{' : 'A punctator', '}' : 'Another punctuator' }";

I'm looking for the RegEx that will match both the entire strings in
the variables a and b.


/^.*$/.test(a)

/^.*$/.test(b)
Matching, I prefer to say testing, if a whole string is ment,
is not that difficult.

You would also have to define when the test should return false.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 15 '06 #6
"CB" <bo*****@hotmail.com> writes:
Sorry I'm not explaining this well. I'm only referring to matching a
string. The string just happens to be code for a obj literal. The
object that the literal describes is not relevant.

var a = "{ '{' : 'LeftCurly', '}' : 'RightCurly' }";
var b = "{ '{' : 'A punctator', '}' : 'Another punctuator' }";

I'm looking for the RegEx that will match both the entire strings in
the variables a and b.


new RegExp(""); // :P

But seriously, you are only telling us half the specification by
saying that it must match these. We also need to know what it
shouldn't match.

Should it match strings that does not contain object literals?
Should it match strings containing any object literal, or only
those with single-quote delimited strings as key and value?
Should it match strings with objects literals with other than
two properties?

It's probably painfully obvious to you what you want (that's
when it's hardest to explain :), but try to explain in words,
concisely, what you want to match and what you don't.

An example matching only single-quote delimited strings as keys and
values, and any number of properties:

/^\{\s*('[^'\\]*(\\.[^'\\]*)*'\s*:\s*'[^'\\]*(\\.[^'\\]*)*'(\s*,\s*'[^'\\]*(\\.[^'\\]*)*'\s*:\s*'[^'\\]*(\\.[^'\\]*)*')*\s*)?\}/

Sometimes a single regular expression is *not* the way to go :)
Instead try writing a real parser.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jun 15 '06 #7
CB wrote:
RegEx matching can only be done on a string,
not on an object or on a any litteral.

Such string could be constructed from a string litteral,
but the matching is on the resulting string.

What has an object litteral to do with regEx?


Sorry I'm not explaining this well. I'm only referring to matching a
string. The string just happens to be code for a obj literal. The
object that the literal describes is not relevant.

var a = "{ '{' : 'LeftCurly', '}' : 'RightCurly' }";
var b = "{ '{' : 'A punctator', '}' : 'Another punctuator' }";

I'm looking for the RegEx that will match both the entire strings in
the variables a and b.


not sure what you exactly wanted, but if you want to capture { '{' :
'LeftCurly', '}' : 'RightCurly' } in var a
, and
{ '{' : 'A punctator', '}' : 'Another punctuator' } in var b

and both keys and values are enclosed by single-quote, and meanwhile
different key-value pairs are separated by comma, then you might try
this pattern:

\{(?:\s*'[^']*'\s*:\s*'[^']*'\s*,?)*\s*\}

if you dont accept empty obj, like var a = "{ }", then change it to:

\{(?:\s*'[^']*'\s*:\s*'[^']*'\s*,?)+\}

if you can take '\'' as the key|value, then replace the above [^']*
with [^'\\]*(?:\\.[^'\\]*)* as shown in Lasse's example.

Xicheng
___
BTW. why not just use [^"]* or [^"\\]*(?:\\.[^"\\]*)*
:-)

Jun 16 '06 #8

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

Similar topics

4
by: engwar1 | last post by:
Not sure where to ask this. Please suggest another newsgroup if this isn't the best place for this question. I'm new to both vb.net and regex. I need a regular expression that will validate what...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
2
by: Tim Conner | last post by:
Hi, Thanks to Peter, Chris and Steven who answered my previous answer about regex to split a string. Actually, it was as easy as create a regex with the pattern "/*-+()," and most of my string...
6
by: Du Dang | last post by:
Text: ===================== <script1> ***stuff A </script1> ***more stuff <script2> ***stuff B
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
5
by: Chris | last post by:
How Do I use the following auto-generated code from The Regulator? '------------------------------------------------------------------------------ ' <autogenerated> ' This code was generated...
6
by: Martin Evans | last post by:
Sorry, yet another REGEX question. I've been struggling with trying to get a regular expression to do the following example in Python: Search and replace all instances of "sleeping" with "dead"....
7
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
6
by: Phil Barber | last post by:
I am using Regex to validate a file name. I have everything I need except I would like the dot(.) in the filename only to appear once. My question is it possible to allow one instance of character...
6
by: | last post by:
Hi all, Sorry for the lengthy post but as I learned I should post concise-and-complete code. So the code belows shows that the execution of ValidateAddress consumes a lot of time. In the test...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.