Regular Expression pattern

K

Kelmen Wong

Greeting,

I want to extract all "[XXX]" from a string, what pattern should I
used?

eg.
[test1] = [test2]

- return array [test1] or test1, [test2] or test2

I tried \[(.*)\] , but it returned the whole "[test1] = [test2]"!

Anyone can help? Also, got any good reference sites especially with
rich example for this patterning?
 
J

Janwillem Borleffs

Kelmen Wong said:
Greeting,

I want to extract all "[XXX]" from a string, what pattern should I
used?

var string = "[test1] = [test2]";
var reg = /\[[^\]]+\]/g;
var matches = string.match(reg);

for (m in matches) alert(matches[m]);


JW
 
L

Lasse Reichstein Nielsen

I want to extract all "[XXX]" from a string, what pattern should I
used?

You want to have a string, and then create an array of all strings in
it that are delimitered by "[" and "]"?
eg.
[test1] = [test2]

- return array [test1] or test1, [test2] or test2

I tried \[(.*)\] , but it returned the whole "[test1] = [test2]"!

Yes, regular expressions try to match as much as possible.
The trick is to not use "." between the two brackets, but [^\]], i.e.,
matchin everything except an end bracket.

Try
function extractBracketed(str) {
var result = [];
var re = /\[([^\]]*)\]/g ;
var match;
while ((match = re.exec(str))) {
result.push(match[1]);
}
return result;
}

/L
 
P

peter seliger

(e-mail address removed) (Kelmen Wong) wrote in message
<html>

<head>
<title>none-greedy RegExp-pattern</title>
<script type="text/javascript">
<!--
/*
hi Kelmen,

eg.
[test1] = [test2]

- return array [test1] or test1, [test2] or test2

I tried \[(.*)\] , but it returned the whole "[test1] = [test2]"!

thats due to the greedy nature of RegExp-patterns;

and "greedy" means:
a pattern alway tries to take as much chracters of
a given string as long as the search rule will match;

in your example "[test1] = [test2]" the last possible
character that will fit with the RegExp /\[(.*)\]/;
is the closing square-bracket of "...test2]" because
of ".*" that allowes as much characters as possible
between an opening and a closing square-bracket;

none-greedy patterns will solve your problem;

you might play with the following lines:
*/

var givenString = "test1 [test2] test3] [test4] [test5] [test6
[test7 test8]";
var regExpression = /\[[^\]]*\]/g;
// look for a opening square bracket - \[ -
// then for everything that is not a closing square bracket -
[^\]] - as often as you can - * -
// till you find a closing square bracket - \] -
// and use this pattern for the entire string - g -
var matches = [];

if (regExpression.test(givenString)) {
matches = givenString.match(regExpression);
}
alert("matches.length = " + matches.length +
"\nmatches.join(\"\\n\") :\n" + matches.join("\n"));


regExpression = /\[[^\[\]]*\]/g;
// look for a opening square bracket - \[ -
// then for everything that is neither a opening nor a closing
square bracket - [^\[\]] - as often as you can - * -
// till you find a closing square bracket - \] -
// and use this pattern for the entire string - g -
if (regExpression.test(givenString)) {
matches = givenString.match(regExpression);
}
alert("matches.length = " + matches.length +
"\nmatches.join(\"\\n\") :\n" + matches.join("\n"));

givenString = "[test1] = [test2]";
if (regExpression.test(givenString)) {
matches = givenString.match(regExpression);
}
alert("matches.length = " + matches.length +
"\nmatches.join(\"\\n\") :\n" + matches.join("\n"));

// if you like to free your matches from its enclosing brackets
treat your "matches"-array as shown right now:
//
// matches = matches.join("splitnjoin");alert(matches);
// matches = matches.replace(/\[/g,"");alert(matches);
// matches = matches.replace(/\]/g,"");alert(matches);
// matches = matches.split("splitnjoin");alert(matches);
//
matches = matches.join("splitnjoin").replace(/\[/g,"").replace(/\]/g,"").split("splitnjoin");

alert("free from enclosing brackets\n\nmatches.join(\"\\n\") :\n"
+ matches.join("\n"));

// NOTE:
//
// in javascript 1.5 none-greedy search patterns can easly
// be written by using the punctuation character "?";
//
// /\[[^\]]*\]/g; then will be /\[.*?\]/g;

/*
givenString = "test1 [test2] test3] [test4] [test5] [test6 [test7
test8]";
regExpression = /\[.*?\]/g; // was: /\[[^\]]*\]/g;

if (regExpression.test(givenString)) {
matches = givenString.match(regExpression);
}
alert("matches.length = " + matches.length +
"\nmatches.join(\"\\n\") :\n" + matches.join("\n"));


// /\[[^\[\]]*\]/g; will turn into /\[[^\[]*?\]/g;

regExpression = /\[[^\[]*?\]/g; // was: /\[[^\[\]]*\]/g;

if (regExpression.test(givenString)) {
matches = givenString.match(regExpression);
}
alert("matches.length = " + matches.length +
"\nmatches.join(\"\\n\") :\n" + matches.join("\n"));
*/

//
// have fun - regards - peterS. - (e-mail address removed)
//

//-->
</script>
<head>

<body>
</body>

</html>
 
L

Lasse Reichstein Nielsen

thats due to the greedy nature of RegExp-patterns;

and "greedy" means:
a pattern alway tries to take as much chracters of
a given string as long as the search rule will match;

In modern implementations of Javscrip (i.e., ECMAScript) you can
use a non-greedy * or + operator (like you can in Perl).

The regular expression is then:

/\[.*?\]/

It finds a "[" followed by the shortest possible match of any characters,
followed by a "]".

It doesn't work in, e.g., Netscape 4.
/L
 
P

peter seliger

(e-mail address removed) (Kelmen Wong) wrote in message

<html>

<head>
<title>none-greedy RegExp-pattern</title>
<script type="text/javascript">
<!--
/*
hi Kelmen,

eg.
[test1] = [test2]

- return array [test1] or test1, [test2] or test2

I tried \[(.*)\] , but it returned the whole "[test1] = [test2]"!

thats due to the greedy nature of RegExp-patterns;

and "greedy" means:
a pattern alway tries to take as much chracters of
a given string as long as the search rule will match;

in your example "[test1] = [test2]" the last possible
character that will fit with the RegExp /\[(.*)\]/;
is the closing square-bracket of "...test2]" because
of ".*" that allowes as much characters as possible
between an opening and a closing square-bracket;

none-greedy patterns will solve your problem;

you might play with the following lines:
*/

var givenString = "test1 [test2] test3] [test4] [test5] [test6
[test7 test8]";
var regExpression = /\[[^\]]*\]/g;
// look for a opening square bracket - \[ -
// then for everything that is not a closing square bracket -
[^\]] - as often as you can - * -
// till you find a closing square bracket - \] -
// and use this pattern for the entire string - g -
var matches = [];

if (regExpression.test(givenString)) {
matches = givenString.match(regExpression);
}
alert("matches.length = " + matches.length +
"\nmatches.join(\"\\n\") :\n" + matches.join("\n"));


regExpression = /\[[^\[\]]*\]/g;
// look for a opening square bracket - \[ -
// then for everything that is neither a opening nor a closing
square bracket - [^\[\]] - as often as you can - * -
// till you find a closing square bracket - \] -
// and use this pattern for the entire string - g -
if (regExpression.test(givenString)) {
matches = givenString.match(regExpression);
}
alert("matches.length = " + matches.length +
"\nmatches.join(\"\\n\") :\n" + matches.join("\n"));

givenString = "[test1] = [test2]";
if (regExpression.test(givenString)) {
matches = givenString.match(regExpression);
}
alert("matches.length = " + matches.length +
"\nmatches.join(\"\\n\") :\n" + matches.join("\n"));

// if you like to free your matches from its enclosing brackets
treat your "matches"-array as shown right now:
//
// matches = matches.join("splitnjoin");alert(matches);
// matches = matches.replace(/\[/g,"");alert(matches);
// matches = matches.replace(/\]/g,"");alert(matches);
// matches = matches.split("splitnjoin");alert(matches);
//
matches = matches.join("splitnjoin").replace(/\[/g,"").replace(/\]/g,"").split("splitnjoin");

alert("free from enclosing brackets\n\nmatches.join(\"\\n\") :\n"
+ matches.join("\n"));

// NOTE:
//
// in javascript 1.5 none-greedy search patterns can easly
// be written by using the punctuation character "?";
//
// /\[[^\]]*\]/g; then will be /\[.*?\]/g;

/*
givenString = "test1 [test2] test3] [test4] [test5] [test6 [test7
test8]";
regExpression = /\[.*?\]/g; // was: /\[[^\]]*\]/g;

if (regExpression.test(givenString)) {
matches = givenString.match(regExpression);
}
alert("matches.length = " + matches.length +
"\nmatches.join(\"\\n\") :\n" + matches.join("\n"));


// /\[[^\[\]]*\]/g; will turn into /\[[^\[]*?\]/g;

regExpression = /\[[^\[]*?\]/g; // was: /\[[^\[\]]*\]/g;

if (regExpression.test(givenString)) {
matches = givenString.match(regExpression);
}
alert("matches.length = " + matches.length +
"\nmatches.join(\"\\n\") :\n" + matches.join("\n"));
*/

//
// have fun - regards - peterS. - (e-mail address removed)
//

//-->
</script>
<head>

<body>
</body>

</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top