By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,377 Members | 1,655 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,377 IT Pros & Developers. It's quick & easy.

Regular Expression pattern

P: n/a
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?
Jul 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"Kelmen Wong" <ke****@hotmail.com> schreef in bericht
news:84**************************@posting.google.c om...
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

Jul 20 '05 #2

P: n/a
ke****@hotmail.com (Kelmen Wong) writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3

P: n/a
ke****@hotmail.com (Kelmen Wong) wrote in message news:<84**************************@posting.google. com>...

<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. - ps******@gmx.net
//

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

<body>
</body>

</html>
Jul 20 '05 #4

P: n/a
ps******@gmx.net (peter seliger) writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5

P: n/a
ke****@hotmail.com (Kelmen Wong) wrote in message news:<84**************************@posting.google. com>...
<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. - ps******@gmx.net
//

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

<body>
</body>

</html>
Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.